How do I enable ssl for all controllers in mvc application

前端 未结 2 742
借酒劲吻你
借酒劲吻你 2021-02-13 20:09

I have a MVC 5 app and I installed the ssl certificate and I\'m now connecting with https, but in my code I had to set the \'[requirehttps]\' attribute on the homecontroller lik

相关标签:
2条回答
  • 2021-02-13 20:20

    The [RequireHttps] attribute is inherited, so you could create a base controller, apply the attribute to that, and then derive all your controllers from that base.

    [RequireHttps]
    public abstract class BaseController : Controller
    {}
    
    public class HomeController : BaseController
    {}
    
    public class FooController : BaseController
    {}
    
    0 讨论(0)
  • 2021-02-13 20:25

    Use the RegisterGlobalFilters method in your FiltersConfig.

    public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
            filters.Add(new RequireHttpsAttribute());
        }
    }
    
    0 讨论(0)
提交回复
热议问题