使用WebApi和Controller路由的Url.Action

Jas*_*son 5 asp.net-mvc asp.net-mvc-routing asp.net-mvc-4 asp.net-web-api

我有一个2个控制器,其中一个是WebApi:

public class ListController : ApiController
{        
    public object Remove(string ListId, List<string> ItemIds)
    {
        //removed
    }
}

public class ListController : Controller
{        
    public object Remove(string ListId, List<string> ItemIds)
    {
        //removed
    }
}
Run Code Online (Sandbox Code Playgroud)

我的路线在Global.asax中注册如下:

WebApiConfig.Register(GlobalConfiguration.Configuration);
RouteConfig.RegisterRoutes(RouteTable.Routes);
Run Code Online (Sandbox Code Playgroud)

我的WebApi路由定义为:

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)

通过@ Url.Action("删除","列表")生成网址时,我得到的路径是'/ list/remove'.我期望选择WebApi路由('/ api/list/remove'),因为WebApi路由在其他路由之前注册.

如何让@ Url.Action按预期返回WebApi路由?

Tom*_*ley 4

@Url.Action("Remove", "List", new { httproute = "DefaultApi" })
Run Code Online (Sandbox Code Playgroud)