如何启用在MVC应用程序的所有控制器的SSL [英] How do I enable ssl for all controllers in mvc application

查看:211
本文介绍了如何启用在MVC应用程序的所有控制器的SSL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC 5应用程序,我安装的SSL证书,现在我以https连接,但在我的code,我不得不设置[requirehttps]'属性上像这样HomeController的:

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?

推荐答案

使用 RegisterGlobalFilters 方法在 FiltersConfig

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

这篇关于如何启用在MVC应用程序的所有控制器的SSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆