Updating Request Messages in ASP.NET Handlers and Modules

In an earlier column I remarked about the top 10 Frameworks for web application. In today’s column I thought of getting into code & here something very interesting for ASP.Net developers. Learn on updating request messages in ASP.Net handlers and modules.

In an ASP.NET MVC module, you’re successfully constrained to adding headers to the incoming ask or outgoing reaction. You’ll moreover work with headers in ASP.NET Web API handlers. This code, applied to the incoming request in either enviroment, removes the Accept handler and includes a replacement (accepting that the variable r focuses to a request or response, of course):

r.Headers.Remove(“Accept”);
r.Headers.Add(“Accept”, “text/html”);

In an ASP.NET Web API handler, you’ll be able also replace the content of the incoming request message, like this:

request.Content = new System.Net.Http.StringContent(“{‘custid’:’A123′}”, Encoding.UTF8, “application/json”);

On the off chance that you utilize this strategy, the default show binding process won’t load the parameters in your Action method with the content you’ve just inserted into the message. You’ll need to write code like this in your Action method to recover that content:

public async Task Get(string Id)
{
string result = await Request.Content.ReadAsStringAsync();

The code to supplant the outgoing request message in a Web API message handler is comparative to the code to replace the incoming request:

resp.Content = new System.Net.Http.StringContent(“{‘custid’:’A124′}”, Encoding.UTF8, “application/json”);

Leave a Comment