9

I am using ASP.Net core and I have a requirement to access a specific HTTP Header in a business logic class (not a controller or action).

To provide a full picture of the configuration here, I have a custom ASP.Net Core Middleware which based on some logic will add a value into a custom HTTP Header, it is the value from this header that I need to access in the business logic class.

Currently the way that I achieve this is to inject an HttpContextAccessor, using the following DI registration.

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

In the class which requires access to the HTTP Headers I then request an IHttpContextAccessor using constructor injection and use this to access the relevant HTTP Header.

Doing the above works fine and gives me the results that I require, looking around various articles on the Internet however the general consensus appears to be to avoid using HttpContext.Current in ASP.Net Core.

If the above is the case, is there a better way for my business logic class to access the value that my custom middleware is inserting into a custom HTTP Header?

I should be clear, whilst at present the middleware is storing the required value in a HTTP Header for use by the business logic class, I am open to other methods of the middleware making the required value available to the business logic class if there is a better approach.

Any questions or clarifications, please let me know.

3
  • Some one of the implementations will need to make access to the context to get the value. However that should not be the business layer. Create a service that provides the header value you desire. That class will take the context. The class abstraction would then be the dependency for your business class Commented Mar 16, 2017 at 18:12
  • Actually - it is a service class that is taking in the context at the moment. I should have perhaps been more specific here, but by referring to a business logic class I was really just trying to make it clear that this is not a controller or action. So generally, passing around a service which accesses the HTTP Context seems to be what you are recommending? Commented Mar 16, 2017 at 18:14
  • You can use abstractions like ISomethingProvider in your business logic layer and then implementing & injecting it in asp.net side as SomethingProviderUsingHeadersClass : ISomethingProvider with the help of IHttpContextAccessor and getting required values from headers.This may allow you avoid using http related things like headers in business logic layer Commented Dec 11, 2022 at 18:52

1 Answer 1

5

There is no HttpContext.Current in ASP.Net Core, so it's easy to avoid using it. You would have to implement your own extension method if you wanted it, but the general feeling in the .Net Core community is that it's much better to use IHttpContextAccessor.

In earlier versions of .Net Core an implementation of IHttpContextAccessor was auto registered in the DI container. In more current version you have to register it yourself with the line of code you mentioned:

 services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

Injecting IHttpContext into your method that needs access to the headers is a workable approach. Or if you like you could use a helper method that places a copy of the headers in a simpler structure and then pass that object in to your class since it doesn't really need access to the full HttpContext.

Sign up to request clarification or add additional context in comments.

2 Comments

I wonder if this is feasible without tweaking ASP.Net programming model too much: using a filter or middleware to construct an object from headers and inject it in controllers?
I think it'd be very doable. You could register a AppHeadersAccessor object with the DI container that returns an AppHeaders object for example. You wouldn't even need middleware or a action filter since the AppHeadersAccessor object could receive via constructor injection the HttpContextAccessor object (provided you registered it as I mention in my answer.) So it'd have everything it needs to populate the AppHeaders object with the header info you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.