Last post Jul 26, 2017 07:18 AM by Edward Z
Jul 13, 2017 08:16 PM|ahager|LINK
I am struggling to find a way to enforce a custom header in the Web API I am building using ASP.Net Core. I know we can validate if the custom header entry and value exist but I am curious if a way exists to have this available to the client based on the
WADL definition. Hope this makes sense?
Jul 13, 2017 08:57 PM|maherjendoubi|LINK
Please have a look on this thread https://forums.asp.net/t/2086253.aspx?Add+custom+header+to+every+web+api+call+from+common+place
Jul 14, 2017 06:24 PM|bruce (sqlwork.com)|LINK
asp.net core webapi has no builtin WADL support. its more common to use swagger for this:
there are several asp.net core open source implementations
Jul 14, 2017 07:01 PM|ahager|LINK
Based on this link I am not sure my question was clearly stated. I am not the Client in this situation, I am writing the Web API service and want to require the presence of certain custom headers on every GET, PUT, POST, ETC. that I receive. In WCF I would
have created a base data contract that all other data contracts derive from which would have included this base information. I am looking for the best way to implement something similar with ASP.Net MVC Core and WEB API and I'd prefer to have this information
in custom header elements rather than in the URL routing structure.
Jul 18, 2017 09:33 AM|Edward Z|LINK
>> I am not the Client in this situation
Do you mean you could not control the client? If so, I think it is impossible to add custom header automatically to every request.
Web API request depends on client request.
For WCF scenario, it is different from Web API, WCF client is created based on service side.
Is the header different for every request? If they are all the same, I think you could add header at service side. If not, I think it is impossible except you add it from client side.
Jul 19, 2017 02:15 PM|bruce (sqlwork.com)|LINK
Jul 20, 2017 04:11 PM|ahager|LINK
This is what I guessed but I am having trouble finding a good example or template to follow. Might you know of any links or examples I can reference?
Jul 21, 2017 07:17 AM|Edward Z|LINK
For Asp.net Core middleware, you could refer below link.
#ASP.NET Core Middleware Fundamentals
>>I am writing the Web API service and want to require the presence of certain custom headers on every GET, PUT, POST, ETC.
Do you need custom headers added by client or you accept the custom headers added at web api server side?
Jul 25, 2017 07:57 PM|ahager|LINK
The same header requirements exist on every call. I was guessing middleware was how this was going to go but I am still unclear on how to implement it. I'm really hoping to find or get a concrete example instead of Microsoft's highlevel non-practical example.
Jul 26, 2017 07:18 AM|Edward Z|LINK
For a simple demo, you could try below code to add header in middleware.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory,IAntiforgery antiforgery)
//your other code
//add custom header
app.Use(next => context =>
var headers = context.Request.Headers;
headers.Add("New Header","New Header Value");
//your other code
And then you could try below code to get header in controller.
var header = Request.Headers["New Header"];