简体   繁体   English

WebAPI:为什么这些方法与路由模板匹配?

[英]WebAPI: Why do these methods match the routing template?

I just re-read the Routing in ASP.NET WebAPI document, and unless I'm missing something, by default WebAPI should only match methods that start with the HTTP verb. 我只是重新阅读了ASP.NET WebAPI中的“ 路由”文档,并且除非缺少某些内容,否则默认情况下,WebAPI应该只匹配以HTTP动词开头的方法。 So why do I get this error when doing a POST against /api/mymodels : 那么,为什么对/api/mymodels进行POST时出现此错误:

ExceptionMessage, Multiple actions were found that match the request: 
Post on type MyApp.Controllers.MyModelController
MaterializerFactory on type Pyro.Controllers.MyModelsController
MaterializerFactory on type Pyro.Controllers.MyModelsController
QueryableFactory on type Pyro.Controllers.MyModelsController

Only the first one should match. 只有第一个应该匹配。 Here are the routes from my WebApiConfig.cs: 这是来自我的WebApiConfig.cs的路由:

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

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

Here are the signatures for the erroneously matching methods: 这是错误匹配方法的签名:

public override JSONAPI.Core.IMaterializer MaterializerFactory() {}

public override TM MaterializerFactory<TM>() {}

public override IQueryable<T> QueryableFactory(Core.IMaterializer materializer = null) {}

The only possibly unusual thing about those is that they are inherited from an intermediate subclass of ApiController that I created…though I can't see how that would matter. 关于它们的唯一可能的不寻常之处是它们是从我创建的ApiController的中间子类继承的……尽管我看不出这有什么关系。

None of my methods are decorated with any WebAPI attributes (eg AcceptVerbs , HttpPost , etc.). 我的方法都没有用任何WebAPI属性修饰(例如, AcceptVerbsHttpPost等)。 If I decorate one of the above with [NonAction] it disappears from the list…but I don't know why it's even trying to match methods with those names? 如果我用[NonAction]装饰上面的其中之一,它会从列表中消失……但是我不知道为什么它甚至试图将具有这些名称的方法进行匹配?

Grrrr…okay, then I re-read the Routing and Action Selection in ASP.NET Web API , and buried in there is this gem: Grrrr…好吧,然后我重新阅读ASP.NET Web API中的“ 路由和操作选择” ,并埋藏在其中:

HTTP Methods. HTTP方法。 The framework only chooses actions that match the HTTP method of the request, determined as follows: 框架仅选择与请求的HTTP方法匹配的操作,确定如下:

  • You can specify the HTTP method with an attribute: AcceptVerbs, HttpDelete, HttpGet, HttpHead, HttpOptions, HttpPatch, HttpPost, or HttpPut. 您可以使用属性指定HTTP方法:AcceptVerbs,HttpDelete,HttpGet,HttpHead,HttpOptions,HttpPatch,HttpPost或HttpPut。
  • Otherwise, if the name of the controller method starts with "Get", "Post", "Put", "Delete", "Head", "Options", or "Patch", then by convention the action supports that HTTP method. 否则,如果控制器方法的名称以“ Get”,“ Post”,“ Put”,“ Delete”,“ Head”,“ Options”或“ Patch”开头,则按照惯例,该操作支持该HTTP方法。
  • If none of the above, the method supports POST. 如果以上都不满足,则该方法支持POST。

头爆炸...

Okay, so this doesn't really stop you from decomposing your code into methods, you just can't make any of them public , or you have to put [NonAction] on them. 好的,所以这并没有真正阻止您将代码分解为方法,只是不能将其中的任何一个public ,或者必须将[NonAction]置于它们之上。 Still, I wish that was far more obvious than a bullet buried in the "advanced topics" document. 不过,我希望那比埋在“高级主题”文档中的项目符号更为明显。

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

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