我正在尝试按照教程保护 Asp.net 但是当我尝试放置此行时
GlobalConfiguration.Configuration.Filters.Add(new System.Web.Http.AuthorizeAttribute());
它给了我以下错误
The best overloaded method match for 'System.Web.Http.Filters.HttpFilterCollection.Add(System.Web.Http.Filters.IFilters)' has some invalid agruments
下面是我的全球.asax代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using DatabaseService_WebAPI.App_Start;
namespace DatabaseService_WebAPI
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
System.Data.Entity.Database.SetInitializer(new DatabaseService_WebAPI.Models.ProductContextInitializer());
System.Data.Entity.Database.SetInitializer(new DatabaseService_WebAPI.Models.LocalDBContextInitializer());
System.Data.Entity.Database.SetInitializer(new DatabaseService_WebAPI.Models.GCMClassContextInitializer());
System.Data.Entity.Database.SetInitializer(new DatabaseService_WebAPI.Models.ProductTypeContextInitializer());
GlobalConfiguration.Configuration.Filters.Add(new System.Web.Mvc.AuthorizeAttribute());
WebApiConfig.Configure(GlobalConfiguration.Configuration);
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new System.Web.Mvc.AuthorizeAttribute());
filters.Add(new RequireHttpsAttribute());
}
}
}
我不是 mvc 的专家开发人员。 所以任何人都可以告诉如何添加过滤器?
在global.asax.cs中创建此方法:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
// add filters here
filters.Add(new MyCustomFilter());
}
然后从Application_Start
方法调用它:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}