ASP.NET:如何获取文件的 URL?
我想获取 ASP.NET 中资源的完全限定 URL。
例如:
<LINK rel="shortcut icon" href="<%=GetFaviconPath()%>">
代码隐藏文件现在包含:
private String GetFaviconPath()
{
String url = System.Web.VirtualPathUtility.ToAbsolute("~/Images/clock.ico");
return url;
}
不幸的是,这不起作用,因为它不返回完全限定路径,仅返回相对于服务器的路径:
/Employement/Images/clock.ico
Internet Explorer需要 >完全限定 url,例如:
http://localhost:62119/Employment/Images/clock.ico
http://avenger:81/Employment/Images/clock.ico
http://MyFreeAspDotNetHosting.com/IanBoyd/Employment/Images/clock.ico
如何获取文件的完全限定路径?我已经尝试过VirtualPathUtility
,但我已经没有主意了。
i want to get the fully qualified url to a resource in ASP.NET.
e.g.:
<LINK rel="shortcut icon" href="<%=GetFaviconPath()%>">
with the code-behind file right now containing:
private String GetFaviconPath()
{
String url = System.Web.VirtualPathUtility.ToAbsolute("~/Images/clock.ico");
return url;
}
Unfortunately this doesn't work because it doesn't return the fully qualified path, only the path relative to the server:
/Employement/Images/clock.ico
Internet Explorer requires a fully qualified url, e.g.:
http://localhost:62119/Employment/Images/clock.ico
http://avenger:81/Employment/Images/clock.ico
http://MyFreeAspDotNetHosting.com/IanBoyd/Employment/Images/clock.ico
How can i get the fully qualified path to a file? i've tried VirtualPathUtility
and i'm all out of ideas.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
的结果中
您可以将您所拥有的内容附加到Request.Url.GetLeftPart(UriPartial.Authority)
另外,请查看 System.UriBuilder http://msdn.microsoft.com/en-us/library/wdwhd34a.aspx
You could append what you have to the result of
Request.Url.GetLeftPart(UriPartial.Authority)
Also, have a look at System.UriBuilder http://msdn.microsoft.com/en-us/library/wdwhd34a.aspx
尝试
将相对路径附加到该绝对路径。
Try this
Append your relative path to that absolute path.