HttpContext.Current.Server.MapPath和HttpContext.Current.Request.Url.Authority之间的区别

编程入门 行业动态 更新时间:2024-10-23 15:24:08
本文介绍了HttpContext.Current.Server.MapPath和HttpContext.Current.Request.Url.Authority之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的网站上我正在上传一个音频文件,该文件保存在项目文件夹中。然后在查看页面时,我想检查上传的文件是否存在于项目文件夹中。我该怎么检查呢。我用这个函数来获取URL并检查该文件中是否存在文件

public string GetAbsoluteUrl( string relativePath) { 尝试 { // 使用权限获取主机和端口 // 仅获取主机DnsSafeHost string host = HttpContext.Current.Request.Url.Authority; string protocol = HttpContext.Current.Request.Url.Scheme; return string .Format( {0}:// {1} / {2},protocol,host,relativePath.Trim()); } catch (例外情况) { throw ex; } }

或者只使用HttpContext.Current.Server.MapPath +文件夹名称+文件名并检查文件是否存在..请帮帮我... 我也想知道什么;两者之间的区别

解决方案

使用Server.MapPath:它将相对于您的网站路径(如〜/ uploads / MP3 /)转换为您可以在File.Exists方法:

if (File.Exists(Server.MapPath( 〜/ Uploads / MP3 / + filename))) { ... 文件}

如果你打算放置,你只需要一个绝对的URL直接链接到客户端网页上的文件的永久链接(这不是一个好主意,因为这意味着该文件夹必须是公开可用的e)所示。

Hi, In my website i am uploading an audio file, which is saved in project folder. Then while viewing a page, i want to check whether the uploaded file exists in the project folder. How can i check it.Shall I used this function to get the URL and check whether file existed in that url

public string GetAbsoluteUrl(string relativePath) { try { //Get host and port by using Authority //Get host only by DnsSafeHost string host = HttpContext.Current.Request.Url.Authority; string protocol = HttpContext.Current.Request.Url.Scheme; return string.Format("{0}://{1}/{2}", protocol, host, relativePath.Trim()); } catch (Exception ex) { throw ex; } }

Or just use HttpContext.Current.Server.MapPath + Folder name+ file name and check whether the file exists or not.. Please help me out... I also want to know what;s the difference between the two

解决方案

Use Server.MapPath: It converts a relative-to-your-website path like "~/uploads/MP3/" to a full path specification you can use in the File.Exists method:

if (File.Exists(Server.MapPath("~/Uploads/MP3/" + filename))) { ... do something with the file }

You would only need an absolute URL if you are planning on putting a permanent link to the file directly onto the clients webpage (which isn't a good idea, as it means the folder must be publically available).

更多推荐

HttpContext.Current.Server.MapPath和HttpContext.Current.Request.Url.Authority之间的区

本文发布于:2023-11-09 15:49:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572774.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:区别   Server   Current   HttpContext   MapPath

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!