c# - Web API 和 HTTP 模块

标签 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 Module:

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));

            }
        }
    }

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);            
    }

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])));
    }
}

最佳答案

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

参见 this file 中的 GetQueryNameValuePairs 方法- 他们获取 Uri 并解析其 Query 属性。

所以你有两种选择:

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

关于c# - Web API 和 HTTP 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35624815/

相关文章:

c# - 确定 (x,y,z) 点是否在由点数组定义的形状内

C# 等待和同步 - 我是否误解了什么?

asp.net - 无法使用 PreSendRequestHeaders() 覆盖 IIS 中的 http 缓存 header

c# - 使用企业库异常 block 的 WCF 异常屏蔽

wcf - 如何将裸结果返回给 WCF 客户端

.net - WCF 服务可以像 WEB API 服务一样模块化吗?

c# - 无法将类型为 "object {System.Collections.Generic.List<string[]>}"的对象转换为 ICollection<object>

c# - 在c#中读取内存流的注意事项

c# - 使用 C# 将 XML 转换为平面文件

sql - 错误 - 附加信息 : Format of the initialization string does not conform to specification starting at index 0