3

我在 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
        }
);

对匹配 URL 的所有 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 Method Not Allowed:“请求的资源不支持 http 方法 'POST'。”

我已将 [HttpPost] 注释添加到要调用的方法中,但我缺少将 Http 请求类型添加到路由配置的方法。

我想我正在寻找这样的东西:

defaults: new { 
    controller = "Receipt",
    httpMethod = "POST, // specify http method 
    action = "GetNotes",
    ReceiptID = RouteParameter.Optional
}
4

0 回答 0