我想有一個GET
和同一個路由POST
:的WebAPI路由:結合屬性和HttpConfiguration設置
我已經註冊了以下內容:
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "MyGetMethod",
routeTemplate: "api/v1/users/{user}",
defaults: new
{
controller = "Users",
action = "MyGetMethod"
},
constraints: null,
handler: HttpClientFactory.CreatePipeline(
new HttpControllerDispatcher(config),
routeHandlerFactory.Create()));
config.Routes.MapHttpRoute(
name: "MySetMethod",
routeTemplate: "api/v1/users/{user}",
defaults: new
{
controller = "Users",
action = "MySetMethod"
},
constraints: null,
handler: HttpClientFactory.CreatePipeline(
new HttpControllerDispatcher(config),
routeHandlerFactory.Create()));
我UsersController.cs
包含:
[HttpPost]
public HttpResponseMessage MySetMethod(string user)
{
return new HttpResponseMessage(HttpStatusCode.NotImplemented);
}
[HttpGet]
public HttpResponseMessage MyGetMethod(string user)
{
return new HttpResponseMessage(HttpStatusCode.NotImplemented);
}
這是行不通的。 的GET
工作正常,但是當我使用POST
了的WebAPI仍然重定向到GET
方法,我也得到了錯誤:
"Message": "The requested resource does not support http method 'POST'."
如果我註釋掉GET
方法的註冊,則POST
工作正常。
是因爲我在控制器方法上使用屬性[HttpPost]
,[HttpGet]
之間的組合而不是將它們標記爲constraints
?
如何在同一條路線上有GET
和POST
?
什麼是Web方法有關使用一個方法與檢查'如果(HttpContext.Current.Request.HttpMethod == 「POST」)'? – Marusyk