Web API和HTTP模块

Das*_*Das 11 c# asp.net wcf asp.net-web-api asp.net-web-api2

我们有一个HTTP模块可以解码所有编码请求.它适用于所有WCF请求,但不适用于Web Api请求 - 在Web Api中请求(POST和GET)到达仍然编码的服务

我看到它点击了HTTP模块,但是,仍然可以获得编码的服务.我该如何解决?或者我做错了什么?我知道在Web Api中使用消息处理程序更好,但是HTTP模块假设也可以工作 - 不是吗?

HTTP模块:

public void Init(HttpApplication context)
    {
        context.BeginRequest += new EventHandler(context_BeginRequest);
        context.EndRequest += context_PreSendRequestContent;
    }

    void context_PreSendRequestContent(object sender, EventArgs e)
    {
        string encodedQuerystring = HttpContext.Current.Request.QueryString.ToString();
        if (!string.IsNullOrEmpty(encodedQuerystring))
        {
            System.Collections.Specialized.NameValueCollection col = new System.Collections.Specialized.NameValueCollection();
            col.Add("q", encodedQuerystring);
            WebFunction.CreateQuerystring(HttpContext.Current, col);
        }


    }

    void context_BeginRequest(object sender, EventArgs e)
    {
        string encodedQueryString = String.Empty;
        if (HttpContext.Current.Request.QueryString.Count > 0 && HttpContext.Current.Request.QueryString["q"] != null)
        {

            object _p = HttpContext.Current.Request.QueryString;
            encodedQueryString = HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request.QueryString["q"].ToString());

            string originalQueryString = HttpContext.Current.Server.UrlDecode(WebFunction.Base64Decode(encodedQueryString));

            if (!string.IsNullOrEmpty(originalQueryString))
            {
                WebFunction.CreateQuerystring(HttpContext.Current, WebFunction.ConvertQueryToCollection(originalQueryString));

            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

WebFunction:

 public static void CreateQuerystring(HttpContext context, System.Collections.Specialized.NameValueCollection nameValueCollection)
    {
        // reflect to readonly property
            PropertyInfo isreadonly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
            // make collection editable
            isreadonly.SetValue(context.Request.QueryString, false, null);
            context.Request.QueryString.Clear();
            context.Request.QueryString.Add(nameValueCollection);         
            // make collection readonly again
            isreadonly.SetValue(context.Request.QueryString, true, null);            
    }
Run Code Online (Sandbox Code Playgroud)

Web Api:

  public class NamesController : ApiController
{
    [HttpGet]
    [ActionName("GET_NAMES")]
    public Drugs_ResponseData Get(string q)
    {
//need to add the decode function to get it to work
        string[] arrAmpersant = Commonnn.DecodeFrom64(q).Split('&');

        Names_obj = new Names();
        return _obj.GetResult(Convert.ToInt32(Commonnn.GetValFromEqual(arrAmpersant[0])));
    }
}
Run Code Online (Sandbox Code Playgroud)

Tom*_*ceg 2

看来Web API在请求中没有使用QueryString集合,而是自己解析URL。

请参阅此文件GetQueryNameValuePairs中的方法- 他们获取 Uri 并解析其 Query 属性。

因此,您有两种选择:

  1. 脏的一件事是更改 HTTP 模块中请求的 Uri。我不知道这是否可能,但一些反思可以解决问题。
  2. 更好的方法是使用 Web API 消息处理程序。