WebApi 2.0路由与查询参数不匹配?

我刚刚从AttributeRouting切换到WebApi 2.0 AttributeRouting,并且有一个控制器和动作定义如下:

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)。


这一切都很好 – 但我想将电子邮件属性更改为查询参数。 所以我将控制器更新为:

 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:

 { "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?


根据要求,WebApiConfig的定义如下:

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

谢谢 !

我认为您需要在Route中包含查询参数(及其类型),如下所示:

 [Route("api/invites/{email:string}")] 

使用它将是

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

或者,如果要为查询参数命名:

 [Route("api/invites")] 

使用它将(只要您的方法中有一个电子邮件参数)

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

当您在edhedges中回答时:路径模板不能以’/’或’〜’开头,因此您可以从路径中删除它,如上所述

问题是路由定义的冲突,由于是跨控制器(以及一些路由是’绝对’( ~/ ))而未被注意到。 下面是一个重现结果的示例。

 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,响应为:

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

什么是误导是响应消息。

我想你需要将你的路线声明更改为: [Route("~/api/invites?{email}")]

这是一个相关的链接: http : //attributerouting.net/#route-constraints