Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a .Net Standard library referencing ActionFilters, HTTPContext , HTTPException etc

I have a bunch of API's authored on .Netcore. I have some action attributes that I want to use across all the API's. I was planning to create a standard library project with the action filters and reference the same in all the API projects. However, I am not sure if I can add references to sytem.web. I am getting a bunch of errors about missing properties and am unable to find the HTTPConext and HTTPException types. What is the right way to create reusable actionfilter attributes for Web API's ?

like image 875
Pradeepl Avatar asked Oct 20 '25 14:10

Pradeepl


1 Answers

There are two ways

1) If your library using only web apps you can add to library via nuget

Microsoft.AspNetCore.Mvc.Abstractions for filters

Microsoft.AspNetCore.Http.Abstractions for HttpContext

and creates shared filters and other manipulations with HttpContext in your library

2) Use DI. Creating some interface and use it in library and create implementation class of it in Projects. After that using DI to inject your class where this interface will called.

  • You can use it in Desktop\Web or whatever apps +
  • No libraries required +
  • You not depends on any version of you API +
  • For filters it's not worked -
  • You need implementation of your interface in each project -

example get cookie from HttpContext in your service in .Net Standard library

public interface ICookieAccessor
{
     string GetCookieValueByName(string name);
}

public class SomeServiceThatUsesCookie()
{
     private readonly ICookieAccessor _cookieAccessor;

     public SomeServiceThatUsesCookie(ICookieAccessor cookieAccessor){
           _cookieAccessor = cookieAccessor;
     }

     public string IWonnaCookie(string name){
           return _cookieAccessor.GetCookieValueByName(name);
     }
}

and implement interface in Web Project (thats implementation should be in each projects)

public class CookieAccessor: ICookieAccessor
{
         private readonly IHttpContextAccessor _httpContext;

         public class CookieAccessor(IHttpContextAccessor httpContext){
                _httpContext = httpContext;
         }

         public string GetCookieValueByName(string name){
                if (_httpContext.HttpContext.Request.Cookies.TryGetValue(name, 
                                                              out var value))
                {
                       return value;
                 }
                 return null;
         }
}

and inject it in your WebApps startup file ConfigureServices method

services.AddTransient<ICookieAccessor, CookieAccessor>();
services.AddTransient<SomeServiceThatUsesCookie>();

than in some controller use your service

public class SomeContoller: Controller
{
      private readonly SomeServiceThatUsesCookie _someService;

      public SomeContoller(SomeServiceThatUsesCookie someService){
             _someService = someService;
      }

      public string GetCookieValue(string name){
             return _someService.IWonnaCookie(name);
      }
}
like image 180
itikhomi Avatar answered Oct 22 '25 03:10

itikhomi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!