使用新的 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)
{
...
}
where 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 参数,并且我应该能够从响应对象访问正文。有任何想法吗?
我不认为路线有问题。如果你想这样写的话,你应该可以接受这条路线。如果您没有合适的选项,您将收到该错误MediaTypeFormatter
对于您在请求中发送的内容类型。例如,如果您正在使用JsonMediaTypeFormatter
你需要确保你正在发送
Content-Type: application/json
在你的要求中。但它看起来不像你。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)