Search code examples
asp.netasp.net-mvcroutescontrollerasp.net-apicontroller

How to register apicontroller & controller route


I need to use the following route for all the basic controller of my project :

 routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

and the following route for all the apicontroller action :

 config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

I register those route by using the following code :

        // Code that runs on application startup
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        GlobalConfiguration.Configure(WebApiConfig.Register); 
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

if i register WebApiConfig first, the actions in all the controllers are not found. But if i register RouteTable first, the api endpoint in the apicontroller are not found.

either way it seems like one is erased by the other.

Did i missed something ? Thanks for any help !


Solution

  • As Nkosi suggested, i resolved the conflict by prefixing my API routes like : "api/{controller}/{id}