'C# MVC Route to different Controller map

I have 2 routes. 1e route for normal pages. 2e route for my json response controller (api with session auth)

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "myApi",
            url: "myApi/{controller}/{action}/{id}",
            defaults: new { Controller = "AddressController", id = UrlParameter.Optional },
            new[] { "myfactory_WebPortal.ControllersApi" } <-- different namespace/folder
        );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            new[] { "myfactory_WebPortal.Controllers" }  <-- different namespace/folder
        );
    }

The 'default' route is just default. The 'myApi' should point to a different folder/namespace with its own controllers and a different url starting with 'myapi/...'

Sadly all my routes work on both routes.

HomeController in 'controllers' namespace/folder works both on url 'myapi/home/contact' and '/home/contact' Same for my controller AddressController in 'controllersApi' namespace/folder works both on url 'myapi/adress/search' and 'address/search'. enter image description here

What should I do to separate these routes/controller to work properly.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source