I need to create an absolute URL to specific files in my ASP.NET MVC 4 application. I am currently doing this by generating a relative path via Url.Content
and then using the following extension method to create the absolute path.
我需要在ASP.NET MVC 4应用程序中创建特定文件的绝对URL。我目前正在通过Url.Content生成相对路径,然后使用以下扩展方法创建绝对路径。
public static string Absolute(this UrlHelper url, string relativeUrl)
{
var request = url.RequestContext.HttpContext.Request;
return string.Format("{0}://{1}{2}{3}",
(request.IsSecureConnection) ? "https" : "http",
request.Url.Host,
(request.Url.Port == 80) ? "" : ":" + request.Url.Port,
VirtualPathUtility.ToAbsolute(relativeUrl));
}
When running under the Azure Emulator, the proper URL that I need to create is http://127.0.0.1/myfile.jpg
but when this code executes, the port number comes back as 81 so the URL that is generated is http://127:0.0.1:81/myfile.jpg
. However, if I go to http://127:0.0.1:81/myfile.jpg
it of course doesn't work as the Azure Emulator is listening on port 80, not 81.
在Azure模拟器下运行时,我需要创建的正确URL是http://127.0.0.1/myfile.jpg但是当执行此代码时,端口号将返回81,因此生成的URL为http:/ /127:0.0.1:81/myfile.jpg。但是,如果我转到http:// 127:0.0.1:81 / myfile.jpg,它当然不起作用,因为Azure模拟器正在侦听端口80,而不是81。
I assume this has to do with the built in Azure Emulator/IIS Express load balancer, but I am not sure what change I need to make to my Url.Absolute
method to return an accurate URL.
我认为这与内置的Azure Emulator / IIS Express负载均衡器有关,但我不确定我需要对我的Url.Absolute方法做出哪些更改才能返回准确的URL。
3 个解决方案
#1
7
You can rely on the Host
header that is being sent by the client:
您可以依赖客户端发送的主机头:
public static string Absolute(this UrlHelper url, string relativeUrl)
{
var request = url.RequestContext.HttpContext.Request;
return string.Format("{0}://{1}{2}",
(request.IsSecureConnection) ? "https" : "http",
request.Headers["Host"],
VirtualPathUtility.ToAbsolute(relativeUrl));
}
#2
3
Why not just use @Url.Content("~/myfile.jpg");
? This converts a virtual (relative) path to an application absolute path and works finle in IIS,the emulator and when deployed. See UrlHelper.Content Method
为什么不使用@ Url.Content(“〜/ myfile.jpg”);?这会将虚拟(相对)路径转换为应用程序绝对路径,并在IIS,模拟器和部署时运行finle。请参阅UrlHelper.Content方法
#3
1
I've written a summary post that shows all the options here: http://benjii.me/2015/05/get-the-absolute-uri-from-asp-net-mvc-content-or-action/
我写了一篇摘要帖子,其中显示了所有选项:http://benjii.me/2015/05/get-the-absolute-uri-from-asp-net-mvc-content-or-action/
The quick and simple answer is this:
快速简单的答案是这样的:
var urlBuilder =
new System.UriBuilder(Request.Url.AbsoluteUri)
{
Path = Url.Content("~/path/to/anything"),
Query = null,
};
string url = urlBuilder.ToString();
#1
7
You can rely on the Host
header that is being sent by the client:
您可以依赖客户端发送的主机头:
public static string Absolute(this UrlHelper url, string relativeUrl)
{
var request = url.RequestContext.HttpContext.Request;
return string.Format("{0}://{1}{2}",
(request.IsSecureConnection) ? "https" : "http",
request.Headers["Host"],
VirtualPathUtility.ToAbsolute(relativeUrl));
}
#2
3
Why not just use @Url.Content("~/myfile.jpg");
? This converts a virtual (relative) path to an application absolute path and works finle in IIS,the emulator and when deployed. See UrlHelper.Content Method
为什么不使用@ Url.Content(“〜/ myfile.jpg”);?这会将虚拟(相对)路径转换为应用程序绝对路径,并在IIS,模拟器和部署时运行finle。请参阅UrlHelper.Content方法
#3
1
I've written a summary post that shows all the options here: http://benjii.me/2015/05/get-the-absolute-uri-from-asp-net-mvc-content-or-action/
我写了一篇摘要帖子,其中显示了所有选项:http://benjii.me/2015/05/get-the-absolute-uri-from-asp-net-mvc-content-or-action/
The quick and simple answer is this:
快速简单的答案是这样的:
var urlBuilder =
new System.UriBuilder(Request.Url.AbsoluteUri)
{
Path = Url.Content("~/path/to/anything"),
Query = null,
};
string url = urlBuilder.ToString();