Asp.net mvc 4 如何在MVC4中编写HTTPS路由

Asp.net mvc 4 如何在MVC4中编写HTTPS路由,asp.net-mvc-4,Asp.net Mvc 4,我已经在MVC4的RouteConfig.cs中编写了路由。它可以很好地使用HTTP;i、 e: routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Default", id = UrlParameter.Optional } ); 如何创建HTTPS路由,以便某些页面可以在HTTPS上

我已经在MVC4的RouteConfig.cs中编写了路由。它可以很好地使用HTTP;i、 e:

routes.MapRoute(
     name: "Default",
     url: "{controller}/{action}/{id}",
     defaults: new { controller = "Home", action = "Default", id = UrlParameter.Optional }
);

如何创建HTTPS路由,以便某些页面可以在HTTPS上打开,而某些页面可以在HTTP上打开?

MVC路由仅与URL的路径部分匹配。
它们完全独立于主机或协议


如果您想将某些URL仅限于HTTPS,请将
[RequireHttps]
属性添加到控制器或操作。

正如@SLaks建议的那样,您可以使用
[RequireHttps]
属性来修饰
操作

但是,如果您不想在
操作上强制使用Https,而只要求
路由只匹配Https请求,请尝试添加
路由约束,如下所示:

public class RequireHttpsConstraint : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        return httpContext.Request.IsSecureConnection;
    }
}
然后:


HTTPS与路由无关,这是IIS的设置方式。也许有两个不同的MVC4应用程序?除此之外,使用HTTPS或HTTP是最干净的情况。从一个切换到另一个不是好的做法。
routes.MapRoute("SecuredPlaceOrder",
        "/PlaceOrderSecured",
        new { controller = "Orders", action = "PlaceOrder" },
        new { requireSSL = new RequireHttpsConstraint() }
    );