问题
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