WebApi 2.0 路由与查询参数不匹配? [英] WebApi 2.0 Routes not matching with query parameters?

查看:42
本文介绍了WebApi 2.0 路由与查询参数不匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚从 AttributeRouting 切换到 WebApi 2.0 AttributeRouting,并且已经定义了一个控制器和动作:

I've just switched from AttributeRouting to WebApi 2.0 AttributeRouting, and have got a controller and action defined like so:

public class InvitesController : ApiController
{
    [Route("~/api/invites/{email}")]
    [HttpGet]
    [ResponseType(typeof(string))]
    public IHttpActionResult InviteByEmail(string email)
    {
        return this.Ok(string.Empty);
    }
}

示例查询:

GET: http://localhost/api/invites/test@foo.com

我收到的响应是 200,内容为空(由于 string.Empty).

The response I receive is a 200, with empty content (due to string.Empty).

这一切正常——但我想将 email 属性改为查询参数.所以我将控制器更新为:

This all works fine -- but I want to change the email property to be a query parameter instead . So I update the controller to:

public class InvitesController : ApiController
{
    [Route("~/api/invites")]
    [HttpGet]
    [ResponseType(typeof(string))]
    public IHttpActionResult InviteByEmail(string email)
    {
        return this.Ok(string.Empty);
    }
}

但是现在查询端点时:

GET: http://localhost/api/invites?email=test@foo.com

我收到的响应是 404:

The response I receive is a 404:

{
"message": "No HTTP resource was found that matches the request URI 'http://localhost/api/invites?email=test@foo.com'.",
"messageDetail": "No route providing a controller name was found to match request URI 'http://localhost/api/invites?email=test@foo.com'"
}

有谁知道为什么当参数交换到查询参数而不是内联 url 时它与路由不匹配?

Does anyone know why it doesn't match the route when the parameter is swapped to a query parameter, rather than inline of the url ?

根据要求,WebApiConfig 定义如下:

As requested, WebApiConfig is defined like so:

public static void Register(HttpConfiguration config)
{
    var jsonFormatter = config.Formatters.JsonFormatter;
    jsonFormatter.Indent = true;
    jsonFormatter.SerializerSettings.ContractResolver = new RemoveExternalContractResolver();

    config.MapHttpAttributeRoutes();
}

<小时>

谢谢!

推荐答案

问题是路由定义的冲突,由于是交叉控制器(还有一些路由是绝对"的(~/)).下面是一个重现结果的例子.

Issue is a clash of route definitions, which went unnoticed due to being cross-controller (and also some of the routes being 'absolute' (~/)). Below is an example that reproduces the result.

public class ValuesController : ApiController
{
    [Route("~/api/values")]
    [HttpGet]
    public IHttpActionResult First(string email)
    {
        return this.Ok("first");
    }
}

[RoutePrefix("api/values")]
public class ValuesTwoController : ApiController
{
    [Route("")]
    [HttpGet]
    public IHttpActionResult Second(string email)
    {
        return this.Ok("second");
    }
}

发出请求:

GET: http://localhost/api/values?email=foo

将返回 404 响应:

Will return a 404 with a response of:

{
    "message": "No HTTP resource was found that matches the request URI 'http://localhost/api/values?email=foo'.",
    "messageDetail": "No route providing a controller name was found to match request URI 'http://localhost/api/values?email=foo'"
}

误导性的是响应消息.

这篇关于WebApi 2.0 路由与查询参数不匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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