同时使用属性路由查询字符串不工作

编程入门 行业动态 更新时间:2024-10-27 18:17:49
本文介绍了同时使用属性路由查询字符串不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我用 System.Web.Http.RouteAttribute 和 System.Web.Http.Route prefixAttribute 启用我的Web API应用2清洁的URL。对于大多数我的请求,我可以使用路由(如控制器/参数1 /参数2 ),或者我可以使用查询字符串(如控制器?参数1 =鲍勃和放大器;参数2 =玛丽)。

I'm using System.Web.Http.RouteAttribute and System.Web.Http.RoutePrefixAttribute to enable cleaner URLs for my Web API 2 application. For most of my requests, I can use routing (eg. Controller/param1/param2) or I can use query strings (eg. Controller?param1=bob&param2=mary).

不幸的是,我的控制器之一(也是唯一一个),这将失败。这是我的控制器:

Unfortunately, with one of my Controllers (and only one), this fails. Here is my Controller:

[RoutePrefix("1/Names")] public class NamesController : ApiController { [HttpGet] [Route("{name}/{sport}/{drink}")] public List<int> Get(string name, string sport, string drink) { // Code removed... } [HttpGet] [Route("{name}/{drink}")] public List<int> Get(string name, string drink) { // Code removed... } }

当我提出一个要求要么使用路由,都做工精细。但是,如果我用一个查询字符串,它失败了,告诉我,这条道路并不存在。

When I make a request to either using routing, both work fine. However, if I use a query string, it fails, telling me that that path does not exist.

我曾尝试加入以下到我的 WebApiConfig.cs 类寄存器(HttpConfiguration配置)功能(前和之后的默认路由),但什么也没做:

I have tried adding the following to my WebApiConfig.cs class' Register(HttpConfiguration config) function (before and after the Default route), but it did nothing:

config.Routes.MapHttpRoute( name: "NameRoute", routeTemplate: "{verId}/Names/{name}/{sport}/{drink}", defaults: new { name = RouteParameter.Optional, sport = RouteParameter.Optional, drink = RouteParameter.Optional }, constraints: new { verId = @"\d+" });

所以,为了清楚起见,我希望能够做到这两者:

So for clarity, I would like to be able to do both this:

localhost:12345/1/Names/Ted/rugby/coke localhost:12345/1/Names/Ted/coke

localhost:12345/1/Names?name=Ted&sport=rugby&drink=coke localhost:12345/1/Names?name=Ted&drink=coke

但遗憾的是查询字符串的版本不工作! (

but sadly the query string versions don't work! :(

更新

我已经完全删除第二个动作,现在尝试使用刚刚与可选参数奇异的行动。我已经改变了我的路线属性 [路线({名称} / {饮料} / {运动?})] 托尼建议做运动空的,但是这现在prevents 本地主机:12345/1 /姓名/泰德/被出于某种原因,一个有效的路由焦。查询字符串行为方式一样了。

I've removed the second Action altogether and now trying to use just a singular Action with optional parameters. I've changed my route attribute to [Route("{name}/{drink}/{sport?}")] as Tony suggested to make sport nullable, but this now prevents localhost:12345/1/Names/Ted/coke from being a valid route for some reason. Query strings are behaving the same way as before.

更新2 我现在有一个奇异的动作在我的控制器:

Update 2 I now have a singular action in my controller:

[RoutePrefix("1/Names")] public class NamesController : ApiController { [HttpGet] [Route("{name}/{drink}/{sport?}")] public List<int> Get(string name, string drink, string sport = "") { // Code removed... } }

但仍使用查询字符串没有找到合适的路径,同时使用路由方法一样。

but still, using query strings does not find a suitable path, while using the routing method does.

推荐答案

多的潜心摆弄和谷歌搜索后,我想出了一个'修复'。我不知道这是否是理想的/最佳实践/普通的老错误的,但它解决了我的问题。

After much painstaking fiddling and Googling, I've come up with a 'fix'. I don't know if this is ideal/best practice/plain old wrong, but it solves my issue.

我所做的只是添加 [路线()] 除了路由属性我已经使用。这基本上允许Web API 2路由允许查询字符串,因为这是目前一个有效的途径。

All I did was add [Route("")] in addition to the route attributes I was already using. This basically allows Web API 2 routing to allow query strings, as this is now a valid Route.

一个例子,现在是:

[HttpGet] [Route("")] [Route("{name}/{drink}/{sport?}")] public List<int> Get(string name, string drink, string sport = "") { // Code removed... }

这使得无论本地主机:12345/1 /姓名/泰德/焦和本地主机:1分之12345/名称名称=特德安培;饮料=焦有效。

更多推荐

同时使用属性路由查询字符串不工作

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

发布评论

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

>www.elefans.com

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