我创建了一个WebApi项目,它有多个WebApi控制器。根据我的要求,我必须在我的WebApi项目中添加一个MVC控制器。我的客户端代码正在使用{ControllerName/ActionMethodName}
向该MVC控制器发出ajax请求。我的global.asax
文件中有多条路线:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
routes.MapHttpRoute(
name: "ActionRoute",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
以下是我的ReportServiceBaseController
:
protected virtual ReportServiceBase CreateReportService();
[HttpPost]
public JsonResult LoadDocumentInfo(LoadDocumentInfoRequest request);
[HttpPost]
public JsonResult LoadDocumentMapInfo(LoadDocumentMapInfoRequest request);
[HttpPost]
public JsonResult LoadDocumentMapInfoFull(LoadDocumentMapInfoFullRequest request);
[HttpPost]
public JsonResult LoadPageInfo(LoadPageInfoRequest request);
我在ReportService
控制器中继承了该控制器:
public class ReportServiceController : ReportServiceBaseController
{
protected override PerpetuumSoft.Reporting.WebViewer.Server.ReportServiceBase CreateReportService()
{
return new ServiceClass();
}
}
发出请求的客户端网址:
“http://”+ hostName +“/ ReportService / LoadDocumentInfo”
我认为WebApi路由正在抑制MVC控制器路由。如何将MVC控制器路由与WebApi路由分开?
答案 0 :(得分:0)
您可以在ApiRouteConfig.cs
内创建一个新的WebApi配置类App_Start
。
namespace Mvc4App
{
public class ApiRouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapHttpRoute(
name: "ActionRoute",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
然后,修改Global.asax.cs
,拨打ApiRouteConfig.RegisterRoutes(RouteTable.Routes);