【发布时间】:2014-09-15 20:40:55
【问题描述】:
我正在尝试为网页和 Web API 注册多个路由。这是我的配置:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Registration Courses SPA",
url: "Registration/Courses",
defaults: new {controller = "Registration", action = "Index"});
routes.MapRoute(
name: "Registration Instructors SPA",
url: "Registration/Instructors",
defaults: new { controller = "Registration", action = "Index" });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
和
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate:"api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
这是我在 Global.asax 中注册它们的方法
RouteConfig.RegisterRoutes(RouteTable.Routes);
GlobalConfiguration.Configure(WebApiConfig.Register);
问题是 Web API 路由不工作,我收到 404 错误,除非我先注册 WebAPI 路由,然后 ASP.NET MVC 路由不工作,Web API 路由工作。
【问题讨论】:
-
我完全按照您的方式进行操作(首先是 WebAPI)并且它在这里工作。你还对路由做其他事情吗?
-
我需要两条路线才能工作,但只有先注册才能工作
-
是的,这很清楚 - 这对我有用,所以我想问您是否对尚未显示的路由进行了其他操作?
-
不,只是我展示的内容
标签: c# asp.net asp.net-mvc asp.net-web-api routing