简体   繁体   English

ASP.NET WebAPI返回Http 405

[英]ASP.NET WebAPI returning Http 405

I have a WebApi controller 我有一个WebApi控制器

[RoutePrefix("api/invitations")]
public class InvitationsApiController : ApiController

And an action method: 一个动作方法:

[Route]
[HttpPost]
public IHttpActionResult Create([FromBody] CreateCommand command)

When i try to POST to http://host/api/invitations i get a "“405 Method Not Allowed” 当我尝试POST到http:// host / api / invitations时,我得到一个“”405方法不允许“

But when I use another route like: 但当我使用另一条路线时:

[Route("test")]
[HttpPost]
public IHttpActionResult Create([FromBody] CreateCommand command)

WebApiConfig.cs WebApiConfig.cs

public static void Register(HttpConfiguration config)
{
    // Web API configuration and services
    SetupFormatters(config);

    // Web API routes
    config.MapHttpAttributeRoutes();

    config.Routes.MapHttpRoute(
        name: "DefaultApi", 
        routeTemplate: "api/{controller}/{id}", 
        defaults: new { id = RouteParameter.Optional });
}

And make a POST to http://host/api/invitations/test its working. 并发送到http:// host / api / invitations / test的工作。 I have controllers that works just perfect when requesting to "root". 我有控制器在请求“root”时工作得很完美。

More details on Http error: 有关Http错误的更多详细信息:

HTTP Error 405.0 - Method Not Allowed The page you are looking for cannot be displayed because an invalid method (HTTP verb) is being used. HTTP错误405.0 - 不允许的方法由于正在使用无效方法(HTTP谓词),因此无法显示您要查找的页面。

Detailed Error Information: 
Module    DefaultDocumentModule
Notification    ExecuteRequestHandler  
Handler    StaticFile  Error
Code    0x80070001  
Requested URL   http://localhost:8008/api/invitations  
Physical Path <removed>   
Logon Method  Anonymous  
Logon User    Anonymous

Any suggestions what might be wrong? 有什么建议可能有什么不对吗?

Change your controller RoutePrefix declaration to Route at the controller level: 在控制器级别将控制器RoutePrefix声明更改为Route

[Route("api/invitations")]
public class InvitationsApiController : ApiController

RoutePrefix doesn't add the route to the route table. RoutePrefix不会将路由添加到路由表。 This means, that if you want to use RoutePrefix , you'll have to prefix each action by itself, for example: 这意味着,如果您想使用RoutePrefix ,您必须自己为每个操作添加前缀,例如:

[RoutePrefix("api/invitations")]
public class InvitationsApiController : ApiController

[Route("")]
[HttpPost]
public IHttpActionResult Create([FromBody] CreateCommand command)

The problem is that you're having a physical directory that is matching your route ( /api/invitations ). 问题是你有一个与你的路线匹配的物理目录( /api/invitations )。 Because such physical directory exists, IIS native DirectoryListingModule and DefaultDocumentModule modules (that are not allowing the POST verb) are taking over the request before the TransferRequestHandler (that is used by MVC and WebAPI) is able to handle it. 因为存在这样的物理目录,IIS本机DirectoryListingModuleDefaultDocumentModule模块(不允许POST动词)在TransferRequestHandler (由MVC和WebAPI使用)能够处理它之前接管请求。

There is no reason to put your InvitationsController.cs in the same directory stucture that is going to match the requested route. 没有理由将InvitationsController.cs放在与请求的路由匹配的同一目录结构中。 That's why we have routing in the first place, to be able to register routes dynamically regardless of the physical location of the controller. 这就是为什么我们首先进行路由,以便能够动态注册路由,而不管控制器的物理位置如何。

The common convention (that is also the default WebAPI template structure) is to put your controllers in the ~/Controllers directory (or ~/Controllers/Api ). 通用约定(也是默认的WebAPI模板结构)是将控制器放在~/Controllers目录(或~/Controllers/Api )中。 But you can actually put them in any directory as long as they're getting compiled as part of the project. 但实际上,只要它们被编译为项目的一部分,您就可以将它们放在任何目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM