I am using MVC3 and am trying to serve content from https, the problem is that when I call Url.Content the files are still served from http using a relative url. I thought this problem was addressed in MVC3 but i can't seem to find any solution. Does anybody know if this issue is inherently solved in MVC3 and how to accomplish it or do I need to create my own helper methods to generate absolute Urls based on protocol?
我正在使用MVC3并尝试从https提供内容,问题是当我调用Url.Content时,仍然使用相对URL从http提供文件。我认为这个问题在MVC3中得到了解决,但我似乎无法找到任何解决方案。有没有人知道这个问题是否在MVC3中固有地解决了以及如何实现它或者我是否需要创建自己的辅助方法来生成基于协议的绝对Url?
4 个解决方案
#1
28
You can probably implement your own solution using VirtualPathUtility.ToAbsolute. Probably something like this:
您可以使用VirtualPathUtility.ToAbsolute实现自己的解决方案。可能是这样的:
public static class UrlHelperExtension {
public static string Absolute(this UrlHelper url, string relativeOrAbsolute) {
var uri = new Uri(relativeOrAbsolute, UriKind.RelativeOrAbsolute);
if (uri.IsAbsoluteUri) {
return relativeOrAbsolute;
}
// At this point, we know the url is relative.
return VirtualPathUtility.ToAbsolute(relativeOrAbsolute);
}
}
which you would use like:
您将使用如下:
@Url.Absolute(Url.Content("~/Content/Image.png"))
(Didn't test this myself, feel free to play around to make it work right.)
(没有自己测试,随意玩,以使其正常工作。)
This helps to you to generate absolute URLs for your content files. In order to change the scheme of the resulting URLs, you can create an additional extension method that manipulates the scheme of the given URLs so that they are HTTPS, or something else.
这有助于您为内容文件生成绝对URL。为了更改生成的URL的方案,您可以创建一个额外的扩展方法来操作给定URL的方案,以便它们是HTTPS或其他。
As Khalid points out in the comments, similar extension methods are already available in various open-source projects which you can make use of (given that the license permits). An example one can be found here.
正如Khalid在评论中指出的那样,类似的扩展方法已经可以在各种开源项目中使用,您可以使用(鉴于许可证允许)。这里可以找到一个例子。
#2
15
A solution that doesn't use extension methods or hardcode the protocol, as suggested by @BlackTigerX:
@BlackTigerX建议的不使用扩展方法或硬编码协议的解决方案:
Url.RouteUrl("Default", new { Action = "About" }, Request.Url.Scheme)
as suggested in the following article: http://captaincodeman.com/2010/02/03/absolute-urls-using-mvc-without-extension-methods/
如下文所述:http://captaincodeman.com/2010/02/03/absolute-urls-using-mvc-without-extension-methods/
#3
10
You can use Url.RouteUrl, some of the overloads take a protocol parameter, looks something like this:
你可以使用Url.RouteUrl,一些重载采用协议参数,看起来像这样:
Url.RouteUrl("Product", new { itemId = "123456" }, "https");
Take a look a the overloads and see which one you can use
看看过载,看看你可以使用哪一个
#4
1
If you don't want to "build" the url and just want the full path of the current page, this will do the trick
如果你不想“构建”网址并只想要当前页面的完整路径,那么这就可以了
Context.Server.UrlEncode(Context.Request.Url.AbsoluteUri)
Context.Server.UrlEncode(Context.Request.Url.AbsoluteUri)
I know it's not as elegant as an Extension Method but thought of sharing it for educational purposes
我知道它不像扩展方法那么优雅,但考虑将其分享用于教育目的
#1
28
You can probably implement your own solution using VirtualPathUtility.ToAbsolute. Probably something like this:
您可以使用VirtualPathUtility.ToAbsolute实现自己的解决方案。可能是这样的:
public static class UrlHelperExtension {
public static string Absolute(this UrlHelper url, string relativeOrAbsolute) {
var uri = new Uri(relativeOrAbsolute, UriKind.RelativeOrAbsolute);
if (uri.IsAbsoluteUri) {
return relativeOrAbsolute;
}
// At this point, we know the url is relative.
return VirtualPathUtility.ToAbsolute(relativeOrAbsolute);
}
}
which you would use like:
您将使用如下:
@Url.Absolute(Url.Content("~/Content/Image.png"))
(Didn't test this myself, feel free to play around to make it work right.)
(没有自己测试,随意玩,以使其正常工作。)
This helps to you to generate absolute URLs for your content files. In order to change the scheme of the resulting URLs, you can create an additional extension method that manipulates the scheme of the given URLs so that they are HTTPS, or something else.
这有助于您为内容文件生成绝对URL。为了更改生成的URL的方案,您可以创建一个额外的扩展方法来操作给定URL的方案,以便它们是HTTPS或其他。
As Khalid points out in the comments, similar extension methods are already available in various open-source projects which you can make use of (given that the license permits). An example one can be found here.
正如Khalid在评论中指出的那样,类似的扩展方法已经可以在各种开源项目中使用,您可以使用(鉴于许可证允许)。这里可以找到一个例子。
#2
15
A solution that doesn't use extension methods or hardcode the protocol, as suggested by @BlackTigerX:
@BlackTigerX建议的不使用扩展方法或硬编码协议的解决方案:
Url.RouteUrl("Default", new { Action = "About" }, Request.Url.Scheme)
as suggested in the following article: http://captaincodeman.com/2010/02/03/absolute-urls-using-mvc-without-extension-methods/
如下文所述:http://captaincodeman.com/2010/02/03/absolute-urls-using-mvc-without-extension-methods/
#3
10
You can use Url.RouteUrl, some of the overloads take a protocol parameter, looks something like this:
你可以使用Url.RouteUrl,一些重载采用协议参数,看起来像这样:
Url.RouteUrl("Product", new { itemId = "123456" }, "https");
Take a look a the overloads and see which one you can use
看看过载,看看你可以使用哪一个
#4
1
If you don't want to "build" the url and just want the full path of the current page, this will do the trick
如果你不想“构建”网址并只想要当前页面的完整路径,那么这就可以了
Context.Server.UrlEncode(Context.Request.Url.AbsoluteUri)
Context.Server.UrlEncode(Context.Request.Url.AbsoluteUri)
I know it's not as elegant as an Extension Method but thought of sharing it for educational purposes
我知道它不像扩展方法那么优雅,但考虑将其分享用于教育目的