1

我创建了一个 WebApi 项目,它有多个 WebApi 控制器。根据我的要求,我必须在我的 WebApi 项目中添加一个 MVC 控制器。我的客户端代码正在向该 MVC 控制器发出 ajax 请求{ControllerName/ActionMethodName}。我的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();
    }

}    

发出请求的客户端 URL:

"http://" + 主机名 + "/ReportService/LoadDocumentInfo"

我认为 WebApi 路由正在抑制 MVC 控制器路由。如何将 MVC 控制器路由与 WebApi 路由分开?

4

1 回答 1

0

您可以ApiRouteConfig.csApp_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);

于 2013-03-13T11:45:06.890 回答