WebAPI和属性路由(WebAPI and Attribute Routing)

编程入门 行业动态 更新时间:2024-10-28 14:24:35
WebAPI和属性路由(WebAPI and Attribute Routing)

我似乎无法找到这个问题的答案所以我在这里发布。 这可能是一个基本的路由问题,也许我只是遗漏了一些明显的东西。 我有一个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&

本文发布于:2023-08-04 10:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415371.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   属性   WebAPI   Attribute   Routing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!