简体   繁体   English

如何使一个路由指向两个不同的控制器端点,该端点在WEB Api 2中接受不同的参数

[英]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) { ... }

And I think this link must be helpful 而且我认为此链接必须有所帮助

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.

相关问题 如何在API控制器中有两个名称相同但参数不同的方法? - How to have two methods in API Controller with same name but different arguments? 是否可以在树中的两个点存在名称空间名称? - Is it OK to have a namespace name which exists at two points in the tree? 是否可以有一个指向两个不同页面的URL? - Is it possible to have a single URL that points to two different pages? 使用具有两种相同但名称不同的两种类型的API。 如何重用我的代码? - Consuming an API which has a two types which are the same but have different names. How can I reuse my code? .net web API:如何从代表 API 操作的控制器类方法中检索路由/路由模板? - .net web API: How to retrieve routes/route templates from controller class method which represents an API operation? 如何创建一个最多接受4个参数的构造函数? - How to create a constructor which accepts up to 4 arguments? ASP.NET Core Web Api 控制器端点都返回 404 错误 - ASP.NET Core Web Api controller end points are all returning 404 error Web API:路由与控制器名称不同,可以吗? - web api: Route different from name of controller, possible? 与Web API应用程序中的控制器名称不同的路由模板 - Different route template than the Controller Name in Web API Application 在Web Api控制器中创建一个指向MVC项目控制器的网址 - Create a Url in Web Api controller that points to MVC project controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM