[MVC]使用Fiddler測試MVC Web API Post要注意的事

[MVC]使用Fiddler測試MVC Web API Post要注意的事

最近寫了一個測試的ASP.NET MVC Web API程式,如下,

Model,


public class CustomerSummary
{
    public string Name { get; set; }
    public string Active { get; set; }
    public string ServiceLevel { get; set; }
    public string OrderCount { get; set; }
    public string MostRecentOrderDate { get; set; }
}

 

Web API,


public HttpResponseMessage Post(CustomerSummary model)
{
    var resp = Request.CreateResponse(HttpStatusCode.Created, model);
    resp.Headers.Location = new Uri(Request.RequestUri, "/api/ApiA/"
        + model.Name);
    return resp;
}

 

然後使用Fiddler測試Post時,接進來的參數都是null,但是如果用Ajax去Post又是正常的。

image

 

後來發現原來是需要在Fiddler的Request Headers中手動加入「Content-Type: application/json」才會正常,如下圖,

image

image

所以,如果Web API接的是JSON格式的話,要記得加入Content-Type: application/json

Hi, 

亂馬客Blog已移到了 「亂馬客​ : Re:從零開始的軟體開發生活

請大家繼續支持 ^_^