Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

What are filters in ASP.Net Web API

In this tutorial, you will learn what are filters in web api, types of filters available in asp.net web api with example. In the previous article, I have explained How to create Web API in ASP.NET C#, Web API CRUD Operations using ASP.NET MVC and Entity Framework, Pass Multiple Parameters in Web API URL C#.

What are filters in Web API?

When you talking about the filters so filters we use to check the authenticity, authorization, you can use filters for converting the result even you can use the filters for other purposes as well. Let say if you are willing to log the user activity. So now understand what are the various types of filters in asp.net web api.

ASP.NET Web API Filter Pipeline
  • Authentication Filter

An authentication filter helps us to authenticate the user detail. In the authentication filter, we write the logic for checking user authenticity.

  • Authorization Filter

Authorization filter is used to check the user’s permission whether the user is having permission to access this api or not. So that permission you can apply based upon the user roles that permission you can apply based upon the user id as well. It depends on whats the logic you are writing.

  • Action Filter

Action filter, you can use for tracking user activity details like for converting the return result from the action.

  • Exception Filter

Sometimes there is an exception in the asp.net web api pipeline so the exception filter going to be called. so that exception can be any level it can be within the action code, it can be within the pipeline so this exception filter will help you to track that exception and send the custom message to the client.

  • Override Filter

Sometimes there is a requirement like whatever the filters we are having we need to override. Let say you applied the filter at the controller level but there is an action within a controller where you don’t want to use the authentication filter so we can use the override version of the authentication filter.

So, there are builtin override filter attributes in asp.net web we are having so there is override authentication filter attribute, if you are willing to override the authentication filter there is an override authorization filter attribute, override action filter attribute and override exception filter attribute so using the overriding version of the attribute we can override filters here.

Filters in Action with Example

Let see how we can create our custom filters in asp.net web api and how we can configure the filters at various level. We can configure the filters at the Global level in web api config file for every request, the second one is a Controller level to execute within the controller and we can apply the filters on Action level as well to execute a filter only for the specific action.

Lets how we can create all these filters in visual studio. Find the source code below:-

Filters:

using System;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Filters;

namespace WebAPIFilter.Filters
{
    public class CustomAuthenticationFilter : Attribute, IAuthenticationFilter
    {
        public bool AllowMultiple => throw new NotImplementedException();

        public Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }

        public Task ChallengeAsync(HttpAuthenticationChallengeContext context, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }
    }
}

using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;

namespace WebAPIFilter.Filters
{
    public class CustomAuthorizationFilter : Attribute, IAuthorizationFilter
    {
        public bool AllowMultiple => throw new NotImplementedException();

        public Task ExecuteAuthorizationFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func> continuation)
        {
            throw new NotImplementedException();
        }
    }
}

using System;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Filters;

namespace WebAPIFilter.Filters
{
    public class CustomExceptionFilter : Attribute, IExceptionFilter
    {
        public bool AllowMultiple => throw new NotImplementedException();

        public Task ExecuteExceptionFilterAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
        {
            throw new NotImplementedException();
        }
    }
}

ProductController

using System.Collections.Generic;
using System.Web.Http;
using WebAPIFilter.Filters;

namespace WebAPIFilter.Controllers
{
    //[CustomAuthenticationFilter]
    //[CustomAuthorizationFilter]
    public class ProductController : ApiController
    {
        [CustomAuthenticationFilter]
       // [CustomAuthorizationFilter]
        public IEnumerable GetAll()
        {
            return new string[] { "laptop", "mobile", "xbox" };
        }

        [OverrideAuthentication]
        [OverrideAuthorization]
        public string Get(int id)
        {
            return "laptop";
        }
    }
}

Download Source Code

Conclusion

I hope you liked this article on what are filters in web api. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

The post What are filters in ASP.Net Web API appeared first on DotNetTec.



This post first appeared on Asp Dot Net Tricks And Tips, Dot Net Coding Tips, Google Maps API Developer, please read the originial post: here

Share the post

What are filters in ASP.Net Web API

×

Subscribe to Asp Dot Net Tricks And Tips, Dot Net Coding Tips, Google Maps Api Developer

Get updates delivered right to your inbox!

Thank you for your subscription

×