How do I enable ssl for all controllers in mvc application

自闭症网瘾萝莉.ら 提交于 2019-12-04 17:33:22

问题


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 like so:

[RequireHttps]
public class HomeController : Controller
{}

Isn't there a way to set it for the whole application so I don't have to do this for each and every controller I have in the app?


回答1:


Use the RegisterGlobalFilters method in your FiltersConfig.

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
        filters.Add(new RequireHttpsAttribute());
    }
}



回答2:


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
{}


来源:https://stackoverflow.com/questions/24317834/how-do-i-enable-ssl-for-all-controllers-in-mvc-application

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