同时使用属性路由查询字符串不工作 [英] Query string not working while using attribute routing

查看:188
本文介绍了同时使用属性路由查询字符串不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 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/名称名称=特德安培;饮料=焦有效。

这篇关于同时使用属性路由查询字符串不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆