Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk4 min

ASP.NET Core MVC Filters: Choose the Right Hook and Control Execution

MVC filters run after action selection and provide hooks around specific MVC stages. Learn the filter types, execution order, registration cautions, and when middleware or policies are a better fit.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

ASP.NET Core MVC filters run inside MVC after it selects an action. Choose one when behavior needs to run at a particular MVC stage or needs action-specific context; use authorization policies for access rules and middleware for concerns that apply more broadly across requests.

What MVC filters do—and where they run

A filter runs before or after a stage of MVC action execution, letting you add cross-cutting behavior without repeating it in every action. Common uses include logging, configuration, caching, authorization, and error handling. Filters are not middleware: they run within MVC’s action-invocation pipeline, after action selection.

As an Amazon Associate I earn from qualifying purchases.

The simplified order is:

  1. Authorization filters
  2. Resource filters
  3. Model binding
  4. Action filters and action execution
  5. Action-result conversion
  6. Exception filters, when an eligible exception occurs
  7. Result filters and result execution

Resource filters surround most of the remaining MVC pipeline. On the normal outward path, result filters finish before resource filters unwind. Exception filters are not a regular stage through which every request passes; they handle certain unhandled exceptions from earlier MVC work.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose a filter by the stage you need

Authorization filters: decide whether MVC should proceed

Authorization filters run first and can stop the filter pipeline when access is denied. For access rules, Microsoft recommends authorization policies rather than a custom authorization filter. Do not throw an exception expecting an exception filter to handle it: exceptions thrown by authorization filters are not handled by exception filters.

Resource filters: work before model binding or wrap most of MVC

Resource filters run after authorization and before model binding. Their position makes them useful when work must happen before MVC binds the request, or when you need to short-circuit much of MVC processing.

Action filters: inspect or change action arguments and results

Action filters run immediately before and after an action method. Implement IActionFilter for synchronous work or IAsyncActionFilter for asynchronous work. They can inspect or modify action arguments and results.

An action filter can stop action execution by assigning a result to ActionExecutingContext.Result and not invoking the next delegate. The action does not run, nor do subsequent action filters in that chain.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Exception filters: handle selected MVC exceptions

Exception filters can handle certain unhandled exceptions during controller or Razor Page creation, model binding, action filters, and action methods. They do not catch exceptions from resource filters, result filters, or MVC result execution.

Microsoft recommends exception-handling middleware for general error handling. An exception filter is more appropriate when the error response must vary by action—for example, JSON for an API endpoint and HTML for a view.

Result filters: surround view or formatter execution

Result filters run around execution of an action result, such as Razor view processing or API serialization. A standard result filter does not run when an authorization or resource filter short-circuits, or when an exception filter produces a result. Use IAlwaysRunResultFilter or its asynchronous counterpart when the result filter must also cover action results produced by those short-circuit paths.

After result execution, the response may already have been sent. An after-result callback cannot change a response that has already gone out.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Filters, middleware, and policies are not interchangeable

Choose When it fits Why
Authorization policy Access rules Microsoft recommends policies instead of custom authorization filters.
Middleware General request-wide behavior, including general exception handling It applies across the broader ASP.NET Core request pipeline, rather than only within MVC action execution.
MVC filter Behavior tied to an MVC stage or action-specific context It can work with action arguments, action results, model-binding boundaries, or action-specific error presentation.

For example, use an action filter when logic needs the action’s bound arguments; use a resource filter when it must run before model binding; and use a result filter when it must surround view rendering or formatter execution. For a general exception strategy, use middleware rather than relying on exception filters to catch every failure.

On [ApiController] endpoints, automatic model-state validation and 400 responses may already cover the need for a custom model-validation action filter.

How filter scope and order work

Filters can be registered globally through MVC options, placed on a controller or Razor Page model, or applied to a controller action where supported. By default, global filters wrap controller-level filters, which wrap action-level filters. Before code runs from the outer scope inward; after code runs in reverse.

IOrderedFilter.Order takes precedence over scope. Lower order values run before code earlier and after code later. Built-in filters generally use order zero; controller-level filters have a documented int.MinValue order detail. Because order can change the default nesting, inspect both scope and order when diagnosing execution sequence.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Register filters with care

For dependency injection, Microsoft documents ServiceFilterAttribute and TypeFilterAttribute as options. Their construction and registration details differ, so follow Microsoft Learn’s examples for the pattern you choose. Do not assume that every filter registration has request-scoped lifetime.

Adding a filter instance directly instead of its type makes that instance a singleton. Microsoft warns that such an instance is not thread-safe. Avoid storing mutable per-request state on a shared filter instance; keep request-specific data in the filter execution context or in appropriately scoped services.

Related mechanisms for Razor Pages and endpoint handlers

Action filters are not supported in Razor Pages, and filter attributes cannot be applied directly to Razor Page handler methods. Razor Page filters instead surround page handlers. Endpoint filters are a separate mechanism available on actions and route-handler endpoints, but not Razor Pages; do not treat them as another name for MVC action filters.

For interface and metadata details, see Microsoft Learn: Filters in ASP.NET Core, the ASP.NET Core filter API reference, and Microsoft’s ASP.NET Core MVC overview. This explanation follows the ASP.NET Core 10.0 documentation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.