[英]ASP.NET Web API Attribute Routing To Wrong Action
I have applied attribute routing on my controller and it'srouting to wrong action. 我在控制器上应用了属性路由,并且路由到错误的操作。 I don't know where I am getting it wrong.
我不知道我在哪里弄错了。
Here is my controller: 这是我的控制器:
using System.Collections.Generic;
using System.Web.Http;
using System.Web.Http.Description;
using System.Linq;
using System;
namespace Iboo.API.Controllers
{
public class ClientsController : ApiController
{
private readonly IClientRepository _repository;
public ClientsController(IClientRepository repository)
{
_repository = repository;
}
// GET: api/Clients
[Route("api/v1/clients")]
public IEnumerable<Client> Get()
{
//code
}
// GET: api/Clients/5
[HttpGet]
[ResponseType(typeof(Client))]
[Route("api/v1/clients/get/{id}")]
public IHttpActionResult GetClientById(int id)
{
//code
}
// GET: api/Clients/5
[HttpGet]
[ResponseType(typeof(string))]
[Route("api/v1/clients/{id}/emailid")]
public IHttpActionResult GetClientEmailId(int id)
{
//code
}
}
}
I am specifically interested in the GetClientEmailId method. 我对GetClientEmailId方法特别感兴趣。 Below is my WebApiConfig
以下是我的WebApiConfig
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
var container = new UnityContainer();
container.RegisterType<IClientRepository, ClientRepository>(new
HierarchicalLifetimeManager());
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/v1/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
My Global.asax.cs is as follows 我的Global.asax.cs如下
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
In the browser If I type http://localhost:54919/api/v1/clients/?id=1/getemailid it's taking me to http://localhost:54919/api/v1/clients which is not what I want. 在浏览器中如果输入http:// localhost:54919 / api / v1 / clients /?id = 1 / getemailid,它将带我到http:// localhost:54919 / api / v1 / clients ,这不是我想要的。
If I try http://localhost:54919/api/v1/clients/1/getemailid I am getting a 404 error. 如果我尝试http:// localhost:54919 / api / v1 / clients / 1 / getemailid,则会收到404错误。
I am not sure as to what I'm getting wrong. 我不确定我出了什么问题。
You can try using the route prefix on the controller. 您可以尝试在控制器上使用路由前缀。
[RoutePrefix("api/v1/clients")]
public class ClientsController : ApiController
{
// GET: api/Clients/5
[ResponseType(typeof(string))]
[Route("{id:int}/emailid"),HttpGet]
public IHttpActionResult GetClientEmailId(int id)
{
//code
}
}
You said: 你说:
In the browser If I type http://localhost:54919/api/v1/clients/?id=1/getemailid it's taking me to http://localhost:54919/api/v1/clients which is not what I want.
在浏览器中如果输入http:// localhost:54919 / api / v1 / clients /?id = 1 / getemailid,它将带我到http:// localhost:54919 / api / v1 / clients ,这不是我想要的。
From the way your routes are set up, it looks like you need to go to http://localhost:54919/api/v1/client/1/emailid
to get to the route you want 从设置路由的方式来看,您似乎需要转到
http://localhost:54919/api/v1/client/1/emailid
以转到所需的路由
To explain the difference, when you call http://localhost:54919/api/v1/clients/?id=1/getemailid
the route that would match that is something like: 为了说明不同之处,当您致电
http://localhost:54919/api/v1/clients/?id=1/getemailid
,匹配的路由如下所示:
[Route("api/v1/clients")]
public IHttpActionResult GetClientEmailId(string id)
{
//code
}
because you've added the id
parameter as a querystring parameter. 因为您已将
id
参数添加为querystring参数。 In this case, the id
argument would have a value of 1/getemailid
which doesn't make much sense. 在这种情况下,
id
参数的值为1/getemailid
,这没有太大意义。
by using the route parameters (by replacing ?id=1/getemailid
with 1/emailid
) you will actually match the route you want to 通过使用路由参数(通过将
?id=1/getemailid
为1/emailid
),您实际上将匹配您想要的路由
You are calling the wrong URLs according to routes on the actions. 您根据操作路线选择了错误的URL。 you get 404 because the URL you call does not match to any of the route templates you have on your actions
您会收到404,因为您调用的网址与操作中使用的任何路由模板都不匹配
[RoutePrefix("api/v1/clients")]
public class ClientsController : ApiController {
//...other code removed for brevity
[HttpGet]
[Route("")] //Matches GET api/v1/Clients
public IHttpActionResult Get() {
//code
}
[HttpGet]
[ResponseType(typeof(Client))]
[Route("{id:int}")] //Matches GET api/v1/Clients/5
public IHttpActionResult GetClientById(int id) {
//code
}
[HttpGet]
[ResponseType(typeof(string))]
[Route("{id:int}/emailid")] //Matches GET api/v1/Clients/5/emailid
public IHttpActionResult GetClientEmailId(int id) {
//code
}
}
Take note of the expected URLs in the comments 注意注释中的预期URL
You should also read up on Attribute Routing in ASP.NET Web API 2 to get a better understanding of how to do attribute-routing. 您还应该阅读ASP.NET Web API 2中的属性路由,以更好地了解如何进行属性路由。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.