【问题标题】:ASP.NET Web API PUT with catch-all route具有包罗万象的路由的 ASP.NET Web API PUT
【发布时间】:2012-04-07 23:49:22
【问题描述】:

使用新的 web api,是否可以像使用 catch all 路由一样

routes.MapHttpRoute(
   name: "name",
   routeTemplate: "api/{*id}",
   defaults: new { controller = "mycontroller", id = RouteParameter.Optional}
);

使用 PUT 方法?通常使用 PUT 方法,如

public HttpResponseMessage Put(string id, string body)
{
    ...
}

body 是 PUT 请求的正文。但是,通过一条全能路线,这似乎不起作用,我收到了错误

{
    "ExceptionType":"System.ArgumentException",
    "Message":":"No 'MediaTypeFormatter' is available to read an object of type 'String' with the media type ''undefined''.",
    "StackTrace":"..."
}

我的 put 方法的样子

public HttpResponseMessage Put(string id)
{
    ...
}

我认为我应该能够使用 catch all 路由,其中​​路由信息将传递给 id 参数,并且我应该能够从响应对象访问正文。有什么想法吗?

【问题讨论】:

    标签: c# asp.net asp.net-web-api put


    【解决方案1】:

    我认为路线不是问题。如果你想这样写,你应该对那条路线没问题。如果您在请求中发送的 Content-Type 没有合适的 MediaTypeFormatter,您将收到该错误。例如,如果您使用的是JsonMediaTypeFormatter,则需要确保您正在发送

    Content-Type: application/json
    

    在您的请求中。它看起来不像你。

    【讨论】:

    • +1 我同意 - 请求的内容类型是这里的问题。
    猜你喜欢
    • 2011-10-13
    • 2016-08-20
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    • 2020-02-23
    • 2021-08-17
    • 2021-09-09
    • 1970-01-01
    相关资源
    最近更新 更多