我似乎无法找到这个问题的答案所以我在这里发布。 这可能是一个基本的路由问题,也许我只是遗漏了一些明显的东西。 我有一个WebAPI2项目,在控制器中有GetAll()方法。 我正在使用包含RoutePrefix的属性路由。 当我使用Postman测试方法时,它工作正常http:// localhost / api / v1 / Suppressions ,它从我的Mongo DB集合中返回一个JSON列表。 在测试过程中我碰巧测试并在URL中添加了一个参数,以为我会收到“无法找到资源”错误http:// localhost / api / v1 / Suppressions?name = abc但它改为调用GetAll( ) 方法。
在路由期间,参数被忽略,并且URI是唯一使用的东西(当然,除非我有一个专门命名参数的方法,如Get(字符串名称))? 如果是这种情况,是否存在我需要添加到我的GetAll()方法的路由约束,以便在有人无意中使用参数或参数列表调用方法时抛出“找不到资源”错误?
I can't seem to find an answer to this question so I'm posting it here. This may be a basic routing question and perhaps I'm just missing something obvious. I have a WebAPI2 project that has GetAll() method in the controller. I'm using Attribute Routing including a RoutePrefix. When I test the method using Postman it works fine http://localhost/api/v1/Suppressions and it returns a list of JSON from my Mongo DB collection. By chance during testing I happened to test and added a parameter to the URL thinking I would receive a "The resource cannot be found" error http://localhost/api/v1/Suppressions?name=abc but it instead called the GetAll() method.
During routing are parameters ignored and is the URI the only thing used (unless, of course, I have a method that specifically named the parameters like Get(string name))? If that's the case, is there a route constraint that I need to add to my GetAll() method in order to throw the "The resource annot be found" error if someone inadvertently called the method with a parameter or list of parameters?
最满意答案
除非在Uri中指定,否则在路由中将忽略查询参数。 您可以为“Suppressions?name = {name}”添加单独的路线,也可以在现有的通话中处理。
有关Uri匹配的信息,请参阅: UriTemplate.Match
Query parameters are ignored in routing, unless they specified in the Uri. You can either add a separate route for 'Suppressions?name={name}' or handle it within your existing call.
For information on Uri matching, see: UriTemplate.Match
更多推荐
GetAll,方法,method,电脑培训,计算机培训,IT培训"/> <meta name="description&
发布评论