HttpModule更改响应 [英] HttpModule Change Response

查看:79
本文介绍了HttpModule更改响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当调用webservice时,我需要在调用某些操作时更改响应文本。

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

因此我创建了捕获响应并更改它的HttpModule。

Therefore I created HttpModule that catch response and change it.

代码下方:

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()
  {
  }
}

如你所见,我只是清除了Response对象并放了我的文字。

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

你怎么看?

推荐答案

我无法给你一个最佳实践答案,但我自己这样做是因为我从一个输出JSON时old-skool ASPX应用程序,它完美无瑕。

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.

这篇关于HttpModule更改响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆