如何在MVC3中使用https生成绝对url?

发布于 2024-10-29 04:46:05 字数 175 浏览 1 评论 0 原文

我正在使用 MVC3 并尝试从 https 提供内容,问题是当我调用 Url.Content 时,文件仍然使用相对 url 从 http 提供。我认为这个问题已在 MVC3 中解决,但我似乎找不到任何解决方案。有谁知道这个问题是否在 MVC3 中本质上得到解决以及如何实现它,或者我是否需要创建自己的辅助方法来基于协议生成绝对 URL?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

手心的温暖 2024-11-05 04:46:05

您可以使用 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);
  }
}

您会使用如下:(

@Url.Absolute(Url.Content("~/Content/Image.png"))

我自己没有对此进行测试,请随意尝试以使其正常工作。)

这有助于您为内容文件生成绝对 URL。为了更改生成的 URL 的方案,您可以创建一个附加的扩展方法来操作给定 URL 的方案,使它们成为 HTTPS 或其他形式。

正如 Khalid 在评论中指出的那样,您可以使用的各种开源项目中已经提供了类似的扩展方法(只要许可证允许)。可以找到一个示例 此处

You can probably implement your own solution using VirtualPathUtility.ToAbsolute. Probably something like this:

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.

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.

许仙没带伞 2024-11-05 04:46:05

不使用扩展方法或对协议进行硬编码的解决方案,如@BlackTigerX所建议:

Url.RouteUrl("Default", new { Action = "About" }, Request.Url.Scheme)

如以下文章中所建议:http://captaincodeman.com/2010/02/03/absolute-urls-using-mvc-without-extension-methods/

A solution that doesn't use extension methods or hardcode the protocol, as suggested by @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/

神经暖 2024-11-05 04:46:05

您可以使用 Url.RouteUrl,一些重载采用协议参数,看起来像这样:

Url.RouteUrl("Product", new { itemId = "123456" }, "https");

看看重载,看看您可以使用哪一个

You can use Url.RouteUrl, some of the overloads take a protocol parameter, looks something like this:

Url.RouteUrl("Product", new { itemId = "123456" }, "https");

Take a look a the overloads and see which one you can use

蹲墙角沉默 2024-11-05 04:46:05

如果您不想“构建”网址而只想获取当前页面的完整路径,那么这将达到目的

Context.Server.UrlEncode(Context.Request.Url.AbsoluteUri)

I知道它不如扩展方法那么优雅,但考虑出于教育目的共享它

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)

I know it's not as elegant as an Extension Method but thought of sharing it for educational purposes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文