如何使用 Url.Action强制使用 http 或 https
我使用 MvcContrib 中的强类型 Url.Action 方法在我的网站上生成所有 url。
例如:
Url.Action<CategoriesController>(c => c.List())
有没有办法强制该方法生成的url使用http或https?现在它似乎只是创建相对于当前页面的网址。因此,例如,如果我在 https 页面上,它会使我的所有页眉和页脚链接都使用 https,即使我不希望这些页面是安全的。
像这样的东西:
Url.Action<CategoriesController>(c => c.List(), protocol: "https")
I'm using the strongly typed Url.Action method from MvcContrib to do all of my url generation on my site.
For example:
Url.Action<CategoriesController>(c => c.List())
Is there a way to force the urls generated by this method to use http or https? Right now it seems to just be creating urls relative to the current page. So, for example, if I'm on an https page it makes all my header and footer links use https even though I don't want those pages to be secure.
Something like:
Url.Action<CategoriesController>(c => c.List(), protocol: "https")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定这是否完全回答了您的问题,但您可以将
[RequireHttps]
属性添加到您想要在 SSL 下强制执行的任何操作(或控制器)。由于 Url.Action 将根据当前方案/权限创建完全限定的 URL,因此它最初会显示指向 http:// 的链接站点/控制器/操作。但是 RequireHttps 属性将为您将方案切换为 HTTPS,并且一旦在该方案中,Url.Action 将适当地返回 https: //site/controller/action URL。I'm not sure if this totally answers your question, but you can add the
[RequireHttps]
attribute to any action (or controller) you want to force under SSL. Since Url.Action will create the fully-qualified URL based on the current scheme/authority, it will initially show links to http://site/controller/action. But the RequireHttps attribute will switch the scheme over to HTTPS for you, and once within that scheme, Url.Action will appropriately return the https://site/controller/action URLs.不幸的是 MvcContrib 的 Action扩展不支持生成 https URL。
在内部,此方法从 MvcFutures 库调用 Microsoft 的 LinkBuilder.BuildUrlFromExpression 方法,该方法非常简单。它仅支持生成简单的相对链接,不支持普通 Url.Action 方法中内置的许多功能,包括 https 和区域。只要 MvcContrib 扩展在内部依赖此方法,它就不会支持这些附加功能。
如果您想利用这些功能,最好坚持使用 Mvc 的常规 Url.Action。
Unfortunately MvcContrib's Action<T> extension doesn't support generating https URLs.
Internally, this method calls into Microsoft's LinkBuilder.BuildUrlFromExpression method from the MvcFutures library which is very simplistic. It only supports generating simple relative links and doesn't support many of the features build into the normal Url.Action method including https and areas. As long as the MvcContrib extensions rely on this method internally, it won't support these additional features.
You'd be better off sticking with Mvc's normal Url.Action if you want to take advantage of these features.