Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to register a global filter with mvc 6, asp.net 5

I'm trying to register a filter in a simple mvc application. I haven't even really written anything yet outside of the basic filter to test with. I'm using VS 2015 RC and I created the initial application by going to new project -> Asp.net Web Application -> Web API. The problem I'm having is that I can't find a way to register the filter globally.

From earlier versions of MVC I see the GlobalFilters.Filters, but when I try to use that in the new framework it tells me that GlobalFilters can't be found. In previous versions it lived in System.Web.MVC, but I no longer see that in my references, and I can't seem to find it anywhere.

This seems like it should be very simple but so far I haven't found a way to do it.

Here's my project.json

{
  "webroot": "wwwroot",
  "version": "1.0.0-*",

  "dependencies": {
    "Microsoft.AspNet.Mvc": "6.0.0-beta4",
    "Microsoft.AspNet.Server.IIS": "1.0.0-beta4",
    "Microsoft.AspNet.Server.WebListener": "1.0.0-beta4",
    "Microsoft.AspNet.StaticFiles": "1.0.0-beta4"
  },

  "commands": {
    "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000"
  },

  "frameworks": {
    "dnx451": {
      "frameworkAssemblies": {
      }
    },
    "dnxcore50": { }
  },

  "exclude": [
    "wwwroot",
    "node_modules",
    "bower_components"
  ],
  "publishExclude": [
    "node_modules",
    "bower_components",
    "**.xproj",
    "**.user",
    "**.vspscc"
  ]
}
like image 707
Zipper Avatar asked Jul 06 '15 04:07

Zipper


2 Answers

With Beta 8 it is now done via AddMvc as follows:

services.AddMvc(options =>
{
    options.Filters.Add(new YouGlobalActionFilter());
});
like image 87
thisleejones Avatar answered Oct 10 '22 06:10

thisleejones


Example of how you can do it in MVC 6

public void ConfigureServices(IServiceCollection services)
{
   services.AddMvc();
   services.ConfigureMvc(options =>
   {
      options.Filters.Add(new YouGlobalActionFilter());
   }
}
like image 23
Kiran Avatar answered Oct 10 '22 08:10

Kiran