ASP.NET Filters SoftUni Team ASP.NET MVC Introduction

Slides:



Advertisements
Similar presentations
AngularJS Routing Routes, Route Parameters, Templates, Location, Navigation SoftUni Team Technical Trainers Software University
Advertisements

AngularJS Services Built-in and Custom Services SoftUni Team Technical Trainers Software University
ASP.NET MVC Architecture Layouts, Filters, Sections, Helpers, Partial Views, Areas… SoftUni Team Technical Trainers Software University
Asynchronous Web Services Writing Asynchronous Web Services SoftUni Team Technical Trainers Software University
Doctrine The PHP ORM SoftUni Team Technical Trainers Software University
Software Technologies
Version Control Systems
Magento Basics Getting started developing for Magento
Auto Mapping Objects SoftUni Team Database Applications
Databases basics Course Introduction SoftUni Team Databases basics
C# MVC Frameworks – ASP.NET
ASP.NET Essentials SoftUni Team ASP.NET MVC Introduction
Web API - Introduction AJAX, Spring Data REST SoftUni Team Web API
Introduction to MVC SoftUni Team Introduction to MVC
Deploying Web Application
WordPress Introduction
PHP MVC Frameworks Course Introduction SoftUni Team Technical Trainers
PHP Fundamentals Course Introduction SoftUni Team Technical Trainers
JavaScript Applications
Application Architecture, Redux
ASP.NET Integration Testing
ASP.NET Unit Testing Unit Testing Web API SoftUni Team ASP.NET
Mocking tools for easier unit testing
Parsing JSON JSON.NET, LINQ-to-JSON
State Management Cookies, Sessions SoftUni Team State Management
EF Code First (Advanced)
PHP MVC Frameworks MVC Fundamentals SoftUni Team Technical Trainers
C# Databases Advanced with Microsoft SQL Server
OOP Course - Virtual Trip
Spring Filters Spring Interceptors SoftUni Team Spring Interceptors
Software Technologies
EF Relations Object Composition
Entity Framework: Code First
Registration, Login, Thymeleaf
Databases advanced Course Introduction SoftUni Team Databases advanced
C#/Java Web Development Basics
MVC Architecture. Routing
Install and configure theme
Entity Framework: Relations
Caching Data in ASP.NET MVC
The Right Way Control Flow
ASP.NET Razor Engine SoftUni Team ASP.NET MVC Introduction
MVC Architecture, Symfony Framework for PHP Web Apps
Transactions in Entity Framework
ASP.NET MVC Introduction
C# Advanced Course Introduction SoftUni Team C# Technical Trainers
Databases Advanced Course Introduction SoftUni Team Databases Advanced
C# Web Development Basics
Best practices and architecture
Testing with Spring Boot
Design & Module Development
Magento Basics part 2 Modules & Themes Stenik Group Ltd. Magento
Multidimensional Arrays, Sets, Dictionaries
Extending functionality using Collections
ASP.NET REST Services SoftUni Team ASP.NET REST Services
Making big SPA applications
ASP.NET Razor Engine SoftUni Team ASP.NET MVC Introduction
C# Advanced Course Introduction SoftUni Team C# Technical Trainers
Course Overview, Trainers, Evaluation
Scheduled Tasks and Web Socket
Introduction to TypeScript & Angular
CSS Transitions and Animations
Train the Trainers Course
Version Control Systems
JavaScript Frameworks & AngularJS
Polymorphism, Interfaces, Abstract Classes
Lean .NET stack for building modern web apps
JavaScript: ExpressJS Overview
CSS Transitions and Animations
Iterators and Generators
Presentation transcript:

ASP.NET Filters SoftUni Team ASP.NET MVC Introduction Technical Trainers Software University http://softuni.bg

Have a Question? sli.do #CSharpWeb

Types of Filters The ASP.NET MVC framework supports four different types of filters: Authorization Filters: Implements the IAuthorizationFilter attribute. Action Filters: Implements the IActionFilter attribute. Result Filters: Implements the IResultFilter attribute. Exception Filters: Implements the IExceptionFilter attribute. Filters are executed in the order listed above. For example, authorization filters are always executed before action filters and exception filters are always executed after every other type of filter. © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Action Filters Apply pre- and post-processing logic Can be applied to actions and to controllers Global filters can be registered in GlobalFilters. Filters (or in /App_Start/FilterConfig.cs) Name Description OutputCache Cache the output of a controller Authorize Restrict an action to authorized users or roles ValidateAntiForgeryToken Helps prevent cross site request forgeries HandleError Can specify a view to render in the event of an unhandled exception © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Output cache Output cache caches the output of an action for a given period of time. In this case the Chache action will be cached for 5 seconds.

Output cache (2) Another option to apply caching in the whole application is to register an OutputCacheFilter in the FilterConfig and setting it’s duration there.

Handle Error Handle Error give us the ability to show a different view depending on the action and the exception it throws. The default HandleErrorAttribute is registered in the FilterConfig and it searches for a view called Error.

Handle Error In order to allow the usage of user friendly error views, you must enable them. To do so, go the web config and write the following: When the mode is “On”, everybody will see the friendly errors. When it is “RemoteOnly” people from the localhost we see the more developer friendly exception.

Handle Error (2) We can easily configure this, by attaching a HandleError attribute to an action.

Handle Error (3) If we want to take information about the exception, we should receive a HandleErrorInfo model in the View(“AnotherError”)

Custom Action Filter Create a C# class file in /Filters/ Inherit ActionFilterAttribute We can override the following methods: OnActionExecuting(ActionExecutingContext) OnActionExecuted(ActionExecutedContext) OnResultExecuting(ResultExecutingContext) OnResultExecuted(ResultExecutedContext) We can apply our new attribute to a controller, method or globally in GlobalFilters.Filters

Custom Action Filter (2) public class LogAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { … } public override void OnActionExecuted(ActionExecutedContext filterContext) public override void OnResultExecuting(ResultExecutingContext filterContext) public override void OnResultExecuted(ResultExecutedContext filterContext) } [Log] public class DepartmentController : Controller { … }

ASP.NET Filters https://softuni.bg/courses/ © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

License This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" license © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.

Free Trainings @ Software University Software University Foundation – softuni.org Software University – High-Quality Education, Profession and Job for Software Developers softuni.bg Software University @ Facebook facebook.com/SoftwareUniversity Software University @ YouTube youtube.com/SoftwareUniversity Software University Forums – forum.softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.