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

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

问题描述

我正在使用 System.Web.Http.RouteAttributeSystem.Web.Http.RoutePrefixAttribute 为我的 Web API 2 应用程序启用更清晰的 URL.对于我的大多数请求,我可以使用路由(例如 Controller/param1/param2)或我可以使用查询字符串(例如 Controller?param1=bob&param2=mary>).

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 类的 Register(HttpConfiguration config) 函数(在默认路由之前和之后),但它什么也没做:

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! :(

更新

我已经完全删除了第二个 Action,现在尝试仅使用带有可选参数的单个 Action.我已将我的路线属性更改为 [Route("{name}/{drink}/{sport?}")] 正如托尼建议将运动设为可空,但这现在阻止了 localhost:12345/1/Names/Ted/coke 由于某种原因成为有效路线.查询字符串的行为与以前相同.

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.

我所做的只是在我已经使用的路由属性之外添加了[Route("")].这基本上允许 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...
}

这使得 localhost:12345/1/Names/Ted/cokelocalhost:12345/1/Names?name=Ted&drink=coke 都有效.

This makes both localhost:12345/1/Names/Ted/coke and localhost:12345/1/Names?name=Ted&drink=coke valid.

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

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