How to force http or https with Url.Action<tController>

这一生的挚爱 提交于 2019-12-24 08:57:51

问题


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")

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/6206019/how-to-force-http-or-https-with-url-actiontcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!