与捕获所有航线的ASP.NET Web API PUT [英] ASP.NET Web API PUT with catch-all route
问题描述
通过新的Web API,是有可能使用一个catch像
With the new web api, is it possible to use a catch all route like
routes.MapHttpRoute(
name: "name",
routeTemplate: "api/{*id}",
defaults: new { controller = "mycontroller", id = RouteParameter.Optional}
);
与PUT方法?通常情况下,PUT方法用于像
with the PUT method? Typically the PUT method is used like
public HttpResponseMessage Put(string id, string body)
{
...
}
其中,体
是PUT请求的主体。然而,随着包罗万象的路线,这似乎并没有工作,我收到错误
where body
is the body of the PUT request. However with a catch all route, this doesn't appear to work and I'm receiving the error
{
"ExceptionType":"System.ArgumentException",
"Message":":"No 'MediaTypeFormatter' is available to read an object of type 'String' with the media type ''undefined''.",
"StackTrace":"..."
}
在我的put方法看起来像
where my put method looks like
public HttpResponseMessage Put(string id)
{
...
}
我想我应该能够使用包罗万象的路线,这里的路由信息将被传递到id参数,那我应该能够从响应对象访问身上。任何想法?
I figure I should be able to use the catch all route, where the route information will be passed to the id parameter, and that I should be able to access the body from the response object. Any ideas?
推荐答案
我不认为路线的问题。你应该罚款与途径,如果这就是你想怎么写。你会得到错误,如果你没有为你发送请求的Content-Type适当 MediaTypeFormatter
。例如,如果您使用的是 JsonMediaTypeFormatter
您需要确保您发送
I don't think the route is the problem. You should be fine with that route if that's how you want to write it. You will get that error if you do not have an appropriate MediaTypeFormatter
for the Content-Type you are sending in the request. For example, if you are using a JsonMediaTypeFormatter
you need to make sure you are sending
Content-Type: application/json
在您的请求。它看起来并不像你。
in your request. Which it doesn't look like you are.
这篇关于与捕获所有航线的ASP.NET Web API PUT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!