ASP.NET Core in Action by Andrew Lock;

ASP.NET Core in Action by Andrew Lock;

Author:Andrew Lock; [Lock, Andrew]
Language: eng
Format: epub
ISBN: 9781638356455
Publisher: Simon & Schuster
Published: 2021-04-13T05:00:00+00:00


Apply the filter to a controller—Anonymous users could access actions from other controllers, but accessing any action on the protected controller would force them to log in.

Apply the filter globally—Users couldn’t use the app without logging in. Any attempt to access an action or Razor Page would redirect the user to the login page.

Note ASP.NET Core comes with just such a filter out of the box: AuthorizeFilter. I’ll discuss this filter in section 13.2.1, and you’ll be seeing a lot more of it in chapter 15.

As I described in the previous section, you normally create filters as attributes, and for good reason—it makes it easy for you to apply them to MVC controllers, actions, and Razor Pages. In this section you’ll see how to apply LogResourceFilter from listing 13.1 to an action, a controller, a Razor Page, and globally. The level at which the filter applies is called its scope.

Definition The scope of a filter refers to how many different actions it applies to. A filter can be scoped to the action method, to the controller, to a Razor Page, or globally.

You’ll start at the most specific scope—applying filters to a single action. The following listing shows an example of an MVC controller that has two action methods: one with LogResourceFilter and one without.

Listing 13.3 Applying filters to an action method

public class RecipeController : ControllerBase { [LogResourceFilter] ❶ public IActionResult Index() ❶ { ❶ return Ok(); ❶ } ❶ public IActionResult View() ❷ { ❷ return OK(); ❷ } ❷ }

❶ LogResourceFilter will run as part of the pipeline when executing this action.

❷ This action method has no filters at the action level.

Alternatively, if you want to apply the same filter to every action method, you could add the attribute at the controller scope, as in the next listing. Every action method in the controller will use LogResourceFilter, without having to specifically decorate each method.

Listing 13.4 Applying filters to a controller

[LogResourceFilter] ❶ public class RecipeController : ControllerBase { public IActionResult Index () ❷ { ❷ return Ok(); ❷ } ❷ public IActionResult View() ❷ { ❷ return Ok(); ❷ } ❷ }

❶ The LogResourceFilter Is added to every action on the controller.

❷ Every action in the controller is decorated with the filter.

For Razor Pages, you can apply attributes to your PageModel, as shown in the following listing. The filter applies to all page handlers in the Razor Page—it’s not possible to apply filters to a single page handler; you must apply them at the page level.

Listing 13.5 Applying filters to a Razor Page

[LogResourceFilter] ❶ public class IndexModel : PageModel { public void OnGet() ❷ { ❷ } ❷ ❷ public void OnPost() ❷ { ❷ } ❷ }

❶ The LogResourceFilter Is added to the Razor Page’s PageModel.

❷ The filter applies to every page handler in the page.

Filters you apply as attributes to controllers, actions, and Razor Pages are automatically discovered by the framework when your application starts up. For common attributes, you can go one step further and apply filters globally, without having to decorate individual classes.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.