我在RouteConfig.cs中有这个路由配置:
// notes for a receipt
routes.MapHttpRoute(
name: "Get Notes for a receipt",
routeTemplate: "receipt/{ReceiptID}/notes",
defaults: new {
controller = "Receipt",
action = "GetNotes",
ReceiptID = RouteParameter.Optional
}
);
对匹配网址的所有GET请求都会路由到此方法:
/// <summary>
/// retrieve notes for a receipt
/// </summary>
/// <param name="ReceiptID">receipt guid</param>
/// <returns>list of notes</returns>
[HttpGet]
public List<NoteDTO> GetNotes(Guid ReceiptID)
{
try
{
IEnumerable<tNotes> Notes = Retriever.GetNotes(ReceiptID);
return ObjectMapper.MapNotes(Notes);
}
catch (Exception)
{
// TODO more granular error handling
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
}
}
这很好用。但是,我希望能够将POST请求发送到完全相同的URL,然后调用另一种方法。但是我收到405方法不允许:“请求的资源不支持http方法'POST'。”
我已将[HttpPost] Annotation添加到我想要调用的方法中,但我错过了将Http请求类型添加到路由配置的方法。
我想我正在寻找这样的事情:
defaults: new {
controller = "Receipt",
httpMethod = "POST, // specify http method
action = "GetNotes",
ReceiptID = RouteParameter.Optional
}