[英]How to have a Route which points to two different controller end points which accepts different arguments in WEB Api 2
How to have a Route which points to two different controller end points which accepts different arguments in WEB Api 2 I have two different end points declared in controller and as for the REST perspective I have to use the alpha/{aplhaid}/beta format for both the end points , 如何拥有一个指向两个不同的控制器端点的路由,该路由在WEB Api 2中接受不同的参数我在控制器中声明了两个不同的端点,至于REST角度,我必须使用alpha / {aplhaid} / beta格式两个终点
[Authorize]
[HttpPost]
[Route("alpha/{aplhaid}/beta")]
public async Task<HttpResponseMessage> CreateAlpha(Beta beta, string projectId, [FromHeader] RevisionHeaderModel revision)
[Authorize]
[HttpPost]
[Route("alpha/{aplhaid}/beta")]
public async Task<HttpResponseMessage> CreateAlpha(List<Beta> betas, string projectId, [FromHeader] RevisionHeaderModel revision)
Is it possible to use the same router with different parameters which points to 2 different end points in Web API 2? 是否可以使用具有不同参数的同一路由器指向Web API 2中的2个不同端点?
Overload web api
action method based on parameter type is not well supported. 不能很好地支持基于参数类型的重载
web api
操作方法。 But what about attribute based routing
? 但是
attribute based routing
呢?
You can find out a good example here 您可以在这里找到一个很好的例子
Route constraints let you restrict how the parameters in the route template are matched. 路由约束使您可以限制路由模板中参数的匹配方式。 The general syntax is
"{parameter:constraint}"
. 通用语法是
"{parameter:constraint}"
。 For example: 例如:
[Route("users/{id:int}"]
public User GetUserById(int id) { ... }
[Route("users/{name}"]
public User GetUserByName(string name) { ... }
If you really need to have the same route and the same ActionName , you could do it with an IHttpActionSelector
. 如果确实需要相同的路由和相同的ActionName ,则可以使用
IHttpActionSelector
。
public class CustomActionSelector : ApiControllerActionSelector, IHttpActionSelector
{
public new HttpActionDescriptor SelectAction(HttpControllerContext controllerContext)
{
var context = HttpContext.Current;
// Read the content. Probably a better way of doing it?
var stream = new StreamReader(context.Request.InputStream);
var input = stream.ReadToEnd();
var array = new JavaScriptSerializer().Deserialize<List<string>>(input);
if (array != null)
{
// It's an array
//TODO: Choose action.
}
else
{
// It's not an array
//TODO: Choose action.
}
// Default.
var action = base.SelectAction(controllerContext);
return action;
}
public override ILookup<string, HttpActionDescriptor> GetActionMapping(HttpControllerDescriptor controllerDescriptor)
{
var lookup = base.GetActionMapping(controllerDescriptor);
return lookup;
}
}
In your WebApiConfig: 在您的WebApiConfig中:
config.Services.Replace(
typeof(IHttpActionSelector),
new CustomActionSelector());
Example for your an Controller: 控制器示例:
public class FooController: ApiController
{
[HttpPost]
public string Post(string id)
{
return "String";
}
[HttpPost]
public string Post(List<string> id)
{
return "some list";
}
}
The solution has some big downsides , if you ask me. 如果您问我,该解决方案有一些弊端 。 First of, you should look for a solution for using this
CustomActionSelector
only when needed. 首先,您应该寻找仅在需要时使用此
CustomActionSelector
的解决方案。 Not for all controllers as it will create an overhead for each request. 并非适用于所有控制器,因为它将为每个请求增加开销。
I think you should reconsider why you really need two have to identical routes. 我认为您应该重新考虑为什么您真正需要两条必须相同的路线。 I think readability will be suffering if the same route accepts different arguments.
我认为,如果同一条路线接受不同的论点,则可读性将受到影响。 But that's just my opinion.
但那只是我的个人意见。
I would use different routes instead. 我会改用其他路线。
使用一个路由,并从第一个控制器内部调用另一个控制器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.