Gregory Nozik Gregory Nozik -3 years ago 96
ASP.NET (C#) Question

HttpModule Change Response

When invoking webservice I need to change response text when invoking certain operation .

Therefore I created HttpModule that catch response and change it.

Below the code :

public class BeginEnd : IHttpModule
{

public void Init(HttpApplication context)
{
context.EndRequest += (o, e) =>
{
HttpContext currContext = HttpContext.Current;

NameValueCollection collection = currContext.Request.QueryString;

if ( collection.Count > 0
&& collection["op"] != null
&& collection["op"] == "ChangeService" )
{
string xmlOther = "<root>My Test</root>";

currContext.Response.Clear();
currContext.Response.Write(xmlOther);
currContext.Response.End();
}
};

}

public void Dispose()
{
}
}


So as you see, I just clear Response object and put my text.

Is is a proper way to do it ?

It's working , but I think that I missing something

What do you think ?

Answer Source

I can't give you a best-practice answer, but I do this myself for when I am outputting JSON from an old-skool ASPX application, and it works flawlessly.

So my answer is (out of personal experience): nothing wrong with this.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download