ASP.NET Core - Real-World ASP.NET Core MVC Filters RRS feed

  • General discussion

  • Filters are a great, often underutilized feature of ASP.NET MVC and ASP.NET Core MVC. They provide a way to hook into MVC’s action invocation pipeline, which makes them great for pulling common, repetitive tasks out of your actions, as Steve Smith explains.

    Read this article in the August issue of MSDN Magazine

    Monday, August 1, 2016 7:24 PM

All replies

  • Hi,

    The link msdn.magazine.com/mt707525 is not correct. Would you please fix it?



    Wednesday, August 10, 2016 5:59 AM
  • The correct link is: 

    But yes.. hope this get's fixed in the article soon as well. :) 

    Caleb Jenkins - Dallas, TX http://developingUX.com

    Friday, August 12, 2016 5:42 PM
  • But aren't you doubling your SQL Server traffic in the GETter? One query in the filter (ListAsync), and a second query in the body of the controller (GetByIdAsync)?

    I'm all for making code more readable, but when it doubles your most-expensive operation, you've cleaned up the GETter (one instance), at the expense of the users (every GET for the lifetime of the app).

    Saturday, August 13, 2016 7:16 AM
  • I like your use of the action filter ValidateAuthorExists. Is there a way to abstract this class? Is something like [ValidateRecordExists(typeof(Author))] possible? In my case I am not using a repository, but an injected DbContext with something like DbSet<Author> Authors.

    Off the top of my head, it should be possible with reflection. Might there be a better way?

    • Edited by Rhyek Sunday, January 15, 2017 10:43 AM
    Sunday, January 15, 2017 10:38 AM
  • Great article. I now understand them much better. 

    WARNING: The code sample is now for VS17! You need to go back a version or two to get the VS15 version of the sample!


    One problem hindering better understanding; the source code will not load in my  VS  for some reason; I don't think its a .NET Core versioning issue. Never seen this one before.

    Microsoft Visual Studio Enterprise 2015
    Version 14.0.25431.01 Update 3
    Microsoft .NET Framework
    Version 4.6.01055
    Microsoft .NET Core Tools (Preview 2)   14.1.21111.0
    Microsoft .NET Core Tools (Preview 2)

    error  : The default XML namespace of the project must be the MSBuild XML namespace. If the project is authored in the MSBuild 2003 format, please add xmlns="http://schemas.microsoft.com/developer/msbuild/2003" to the <Project> element. If the project has been authored in the old 1.0 or 1.2 format, please convert it to MSBuild 2003 format.  

    I hope this helps

    Thursday, February 23, 2017 11:10 AM
  • Completely agree. It's a small return for a substantial loss.
    Tuesday, March 21, 2017 1:08 AM
  • I completely agree, this is a very bad pattern to follow and saves just three lines of boilerplate code. I also question the use of .All(a.Id !=id) instead of SingleOrDefault.
    Friday, April 14, 2017 12:31 PM
  • I completely agree, this is a very bad pattern to follow and saves just three lines of boilerplate code. I also question the use of .All(a.Id !=id) instead of SingleOrDefault.

    I completely disagree.  It's an example, and somewhat contrived as all examples are - however, it's a perfect example of clean code.  Don't Repeat Yourself - if you've got half-a-dozen Actions on a Controller (Or a few dozen controllers with hundreds of actions), as is quite common, this is saving hundreds of lines of code.  It's ensuring that there's one place to fix an issue, rather than hundreds of repeated places.

    As to the .All || SingleOrDefault - it's an example, the code needs quite a bit more work to make production ready, but it shows good methods to handle this sort of situation.

    Friday, February 23, 2018 9:30 AM