简体   繁体   English

关闭ASP.NET MVC WebApi中的自动解码

[英]Turn AutoDecode in ASP.NET MVC WebApi off

It seems that the FormDataCollection in the ASP.NET MVC Web API auto-decodes the values. 似乎ASP.NET MVC Web API中的FormDataCollection会自动解码这些值。 Is there a way to turn this off? 有没有办法关闭此功能? I am using fiddler to post the following data to my url: 我正在使用提琴手将以下数据发布到我的网址:

.code=2001&Color=Lam%E9%20Purple

Unfortunately the color is already decoded and results into the following: 不幸的是,颜色已经被解码,结果如下:

Lam� Purple

What I was hoping to get is this: 我希望得到的是:

Lamé Purple

Code used: 使用的代码:

[HttpPost]
public HttpResponseMessage PostItem(FormDataCollection collection)
{
    int qty = 5000;
    var response = Request.CreateResponse(HttpStatusCode.Created);
    response.StatusCode = HttpStatusCode.OK;
    if (collection == null)
    {
        response.Headers.Add("Available", qty.ToString());
        return response;
    }

    var item = new Item();
    foreach (KeyValuePair<string, string> keyValuePair in collection)
    {
        switch(keyValuePair.Key)
        {
            case ".quantity":
                item.Qty = Convert.ToInt32(keyValuePair.Value);
                break;
            case ".code":
                item.Style = keyValuePair.Value;
                break;
            case "Color":
                item.Color = keyValuePair.Value;
                break;
            case "Size":
                item.Size = keyValuePair.Value;
                break;
        }
    }

    qty = Repository.GetInventory(item);
    response.Headers.Add("Available", qty.ToString());    
    return response;
}

You seem to be using some specific request encoding, not UTF-8. 您似乎正在使用某些特定的请求编码,而不是UTF-8。 So adapt your web.config accordingly using the <globalization> element: 因此,使用<globalization>元素相应地调整web.config:

<globalization requestEncoding="iso-8859-1" />

or make sure you send correct utf-8 url encoded request: 或确保您发送正确的utf-8 url编码请求:

code=2001&Color=Lam%C3%A9%20Purple

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM