ASP.NET 具有捕获全部路由的 Web API PUT
本文关键字:路由 Web API PUT 全部 NET ASP | 更新日期: 2023-09-27 18:30:27
使用新的 web api,是否可以使用
像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 参数,并且我应该能够从响应对象访问正文。 有什么想法吗?
我不认为路线是问题所在。如果这是您想要编写的方式,您应该可以使用该路线。如果您没有针对在请求中发送的内容类型的适当MediaTypeFormatter
,则会收到该错误。例如,如果您使用的是JsonMediaTypeFormatter
则需要确保发送
Content-Type: application/json
在您的请求中。看起来不像你。