Asp.net mvc 4 HttpContext.Current.Request在RegisterGlobalFilter中不可用

Asp.net mvc 4 HttpContext.Current.Request在RegisterGlobalFilter中不可用,asp.net-mvc-4,Asp.net Mvc 4,我正在尝试将requireHttpAttribute属性添加到MVC过滤器集合,以便在prod server上部署网站时将其推送到HTTPS。问题在于HttpContext.Current.Request.IsLocal行,Request对象尚不可用。那么,如何检查站点是否在RegisterGlobalFilters中的localy或prod服务器上运行 public static void RegisterGlobalFilters(GlobalFilterCollection filters

我正在尝试将
requireHttpAttribute
属性添加到
MVC过滤器集合
,以便在prod server上部署网站时将其推送到HTTPS。问题在于
HttpContext.Current.Request.IsLocal
行,
Request
对象尚不可用。那么,如何检查站点是否在
RegisterGlobalFilters
中的
localy
prod
服务器上运行

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {    
        if (!HttpContext.Current.Request.IsLocal) //Exception here!!!
        {
            filters.Add(new RequireHttpsAttribute());
        }
    }

在这种方法中,您需要注册过滤器,当请求传入时,过滤器将执行检查。每次启动应用程序时,只调用一次此方法。因此,在这里,您需要按照以下思路做一些事情:

filters.Add(new MyAuthorizeAttribute());
 public class MyAuthorizeAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        If(!httpContext.Request.IsLocal)
        {
            **//Check for HTTPS and return false if need be**
        }
}
MyAuthorization属性的含义如下:

filters.Add(new MyAuthorizeAttribute());
 public class MyAuthorizeAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        If(!httpContext.Request.IsLocal)
        {
            **//Check for HTTPS and return false if need be**
        }
}
当然,它不需要是authorized属性

编辑

正如我之前所说的,这个方法在应用程序开始时只调用一次,因此没有要求您在这里签入。在这里,您只能应用在每次收到请求时调用的筛选器。在这些过滤器中,您可以检查特定于请求的属性


如果您坚持使用RequireHttpAttribute,则无论请求是否为本地请求,您都必须将其应用于所有方法,或者您必须扩展RequireHttpAttribute并重写HandlenOnHttpRequest以处理本地请求。

对不起,我不明白您的意思。这如何帮助我找出RegisterGlobalFilters中的请求是本地的,并将RequireHttpAttribute属性添加到过滤器列表中?