18

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"
  ]
}
Zipper
  • 6,684
  • 6
  • 44
  • 64
  • 1
    There's normally a FilterConfig class in the App_Data folder; is that what you're after? – Tieson T. Jul 06 '15 at 05:53
  • Well the FilterConfig takes in the GlobalFilters.Filters, but I can't get the GlobalFilters to start with – Zipper Jul 07 '15 at 00:46
  • Are you trying to register filters purely through the project.json file? Also, you've tagged this MVC5, but what your config file as shown indicates that you're using MVC6; is that correct? – Tieson T. Jul 07 '15 at 01:17
  • Sorry, forgot I was on 6. I've been playing with the latest beta's and got confused. I'm trying to register the filter in the startup.cs that get's auto generated from the VS 2015 RC default web api project under ASP.net 5 preview templates – Zipper Jul 07 '15 at 01:37
  • 1
    Things have definitely changed in MVC 6. I think this article will help you. http://www.strathweb.com/2015/02/overriding-filters-asp-net-mvc-6/ – Joe Audette Jul 07 '15 at 13:25

2 Answers2

33

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

services.AddMvc(options =>
{
    options.Filters.Add(new YouGlobalActionFilter());
});
R E N T B O Y
  • 663
  • 7
  • 14
  • Thanks for the answer! Is it possible to apply filter only for some specific mvc area? (I want my backend be closed for anonymous users, but not frontend.) Thanks! – Dmitry Sikorsky Dec 04 '15 at 14:49
10

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());
   }
}
Kiran Challa
  • 53,599
  • 15
  • 167
  • 151
  • 6
    This answer is no longer valid, see Lee Jones answer (http://stackoverflow.com/a/33768705/11220) – noocyte Nov 25 '15 at 08:49