簡體   English   中英

WebApi POST請求由GET處理

[英]WebApi POST request gets handled by GET

我正在使用RestSharp消耗WebApi。 以下是相關代碼:

var insertRequest = new RestRequest("MappedSystem", Method.POST);
insertRequest.AddBody(new MappedSystemCreateModel
                {
                    MappedSystemDetails = new MappedSystemCreateModel.Details
                    {
                        SystemName = "TestName",
                        SystemVersion = "TV"
                    }
                });
var response = RestClient.Execute(insertRequest);

但是,當我調試WebApi時,它會遇到Get()方法:

public class MappedSystemController : ApiController
{
    private readonly IMappedSystemService _mappedSystemService;

    public MappedSystemController(IMappedSystemService mappedSystemService)
    {
        _mappedSystemService = mappedSystemService;
    }

    public MappedSystemViewModel[] Get()
    {
=>      return _mappedSystemService.Get();
    }

    public MappedSystemViewModel Get(Guid id)
    {
        return _mappedSystemService.Get(id);
    }

    [HttpPost]
    public MappedSystemViewModel Post([FromBody]MappedSystemCreateModel model)
    {
        return _mappedSystemService.Post(model);
    }
}

我認為routeConfig一定有問題,但是我目前不知道:

config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}/{id2}/",
                defaults: new { id = RouteParameter.Optional, id2 = RouteParameter.Optional }
            );

有一個Post路由處理程序攔截器檢查我是否正確添加了結尾的'/'。 由於我沒有,它以301響應並重定向。 在RedirectPermanently()中的某處/某處動詞已丟失。 答案是最初以“ /”結尾發出請求。 但這當然可以說明重定向中發現的錯誤。

另外,我在請求上需要以下代碼,否則主體將不會反序列化:

insertRequest.RequestFormat = DataFormat.Json;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM