2

使用新的 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)
{
    ...
}

bodyPUT 请求的主体在哪里。但是,通过一条全能路线,这似乎不起作用,我收到了错误

{
    "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 参数,并且我应该能够从响应对象访问正文。有任何想法吗?

4

1 回答 1

2

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

Content-Type: application/json

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

于 2012-05-18T12:15:21.203 回答