1
votes

I have created a WebApi project, which has multiple WebApi controllers. As per my requirement I have to add an MVC controller to my WebApi project. My client side code is making an ajax request to that MVC controller with {ControllerName/ActionMethodName}. I have multiple routes in my global.asax file:

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 }
);

Below is my 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);

I am inheriting that controller in a ReportService controller:

public class ReportServiceController : ReportServiceBaseController
{
    protected override PerpetuumSoft.Reporting.WebViewer.Server.ReportServiceBase   CreateReportService()
    {
        return new ServiceClass();
    }

}    

The client side URL which is making the request:

"http://" + hostName + "/ReportService/LoadDocumentInfo"

I think the WebApi route is suppressing the MVC Controller route. How can I separate the MVC Controller route from the WebApi route?

1
Can you show us the request URL for which you think the route is suppressed (in general they way the routes are created is preventing suppressing through the api part in the URL). - tpeczek
I have edited question with code. - user1557847

1 Answers

0
votes

You can create a new config class of WebApi, ApiRouteConfig.cs, within 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 }
            );
        }
    }    
}

Then, edit Global.asax.cs, to call ApiRouteConfig.RegisterRoutes(RouteTable.Routes);