asp.net-mvc - 带有 Web Api 和 Controller 路由的 Url.Action

标签 asp.net-mvc asp.net-mvc-4 asp.net-web-api asp.net-mvc-routing

我有 2 个 Controller ,其中 1 个是 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
    }
}

我的路线在 Global.asax 中注册如下:
WebApiConfig.Register(GlobalConfiguration.Configuration);
RouteConfig.RegisterRoutes(RouteTable.Routes);

我的 WebApi 路由定义为:
config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

通过@Url.Action("Remove", "List")生成url时,得到的路径是'/list/remove'。我希望选择 WebApi 路由('/api/list/remove'),因为 WebApi 路由是在其他路由之前注册的。

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

最佳答案

@Url.Action("Remove", "List", new { httproute = "DefaultApi" })

关于asp.net-mvc - 带有 Web Api 和 Controller 路由的 Url.Action,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24868881/

相关文章:

asp.net-mvc - MVC ASP.NET 没有为此对象定义无参数构造函数

asp.net-mvc - 使用 Unity 的 IDependencyResolver.GetServices 是否应该返回默认(未命名)注册

asp.net-mvc - 如何在 MVC Razor View 中使用/显示 Asp.net DataList

c# - OnCertificateValidated 未运行 - 自签名证书客户端身份验证 - ASP.NET Core 和 Kestrel

javascript - 通过 javascript 从另一个应用程序访问 MVC Action

asp.net-mvc - 使用默认的 ASP.Net MVC 设置,如何使用 Microsoft 帐户登录?

asp.net - 程序集中的“System.Data.SqlClient.SqlConnection”未标记为可序列化?

c# - Membership.DeleteUser 没有删除用户的所有相关行

c# - 生成和流式传输 Zip 存档,而不将其存储在文件系统中或先读入内存

asp.net - 来自 Web API 的 XML 响应中的 CData