简体   繁体   English

Web API:路由与控制器名称不同,可以吗?

[英]web api: Route different from name of controller, possible?

I have mineController.cs now in my route, is it possible to do /api/mycontroller/myaction 我的路线中现在有mineController.cs,可以执行/ api / mycontroller / myaction

ie I try to get route not limit to controller name 即我尝试获取路由不限于控制器名称

Yes, by using Attribute Routing. 是的,通过使用属性路由。

Step 1: Enable attribute routing in WebApiConfig.Register method (might be on by default, I don't recall offhand): 第1步:在WebApiConfig.Register方法中启用属性路由(默认情况下可能处于启用状态,我不会立即记起):

config.MapHttpAttributeRoutes();

Step 2: Not required, but it's nice to use a RoutePrefix attribute for the entire controller: 步骤2:不是必需的,但是最好对整个控制器使用RoutePrefix属性:

[RoutePrefix("api/mycontroller")
public class mineController : ApiController
{
    ..
}

Step 3: Use a Route attribute on each method that completes the route prefix: 步骤3:在每个用于完成路由前缀的方法上使用Route属性:

[Route("myaction")]
[HttpGet] /* or other HttpVerb */
public IHttpActionResult SomeMethod()
{
   ...
}

[Route("myaction/{id}")]
[HttpGet] /* or other HttpVerb */
public IHttpActionResult SomeMethod(int id)
{
   ...
}

More info here: https://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2 此处提供更多信息: https : //www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

If you are using Web Api 2 you can use Attribute Routing for this: 如果您使用的是Web Api 2,则可以为此使用“ 属性路由 ”:

[RoutePrefix("api/mine")]
public class mineController : ApiController
{
    [Route("method1")]
    [HttpGet]
    public IHttpActionResult Method1()
    {
        //Route would be api/mine/method1
    }

    [Route("method2")]
    [HttpGet]
    public IHttpActionResult Method2()
    {
        //Route would be api/mine/method2
    }
}

You can use the System.Web.Http.RouteAttribute to decorate your controller actions and specify any route you desire. 您可以使用System.Web.Http.RouteAttribute装饰您的控制器操作并指定所需的任何路由。 Depending on what it is you want to do, this may be a good approach for you. 根据您要执行的操作,这可能是您的一种好方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 与Web API应用程序中的控制器名称不同的路由模板 - Different route template than the Controller Name in Web API Application 路由前缀VS控制器名称(Web api) - Route Prefix VS Controller Name ( Web api ) 修改API控制器的路由以从控制器名称中删除“ API” - Modify route of API controller to remove “API” from the controller name Web API 2.2,具有路由覆盖的继承控制器(这可能)吗? - Web API 2.2, Inherited Controller with Route Overrides (Is this possible)? Web API控制器按路线选择 - Web api controller selection by route Web Api 获取路由中的名称 - Web Api get the Name in the Route Web API路由到操作名称 - Web API route to action name 如何使一个路由指向两个不同的控制器端点,该端点在WEB Api 2中接受不同的参数 - How to have a Route which points to two different controller end points which accepts different arguments in WEB Api 2 .NET Core Web API:可以在appsettings.json配置文件中分配控制器路由吗? - .NET Core Web API: Possible to assign controller route in appsettings.json config file? 没有路由名称,带有属性路由的ASP.NET Web API控制器不起作用 - ASP.NET Web API controller with attribute routing doesn't work without a route name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM