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

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

问题描述

我刚刚从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).

这一切工作正常 - 但我想改变的电子邮件属性为查询参数来代替。所以我更新控制器:

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);
    }
}

但随着查询端点现在时:

But now when querying the endpoint with:

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'"
}

什么是误导性的是响应消息。

What is misleading is the response message.

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

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