Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Catch All Route Breaks Dynamic API #2073


User avatar
0
rickwheeler created

Hi,

If I define a route like the following underneath all routes in in the RouteConfig.cs, calls from the angular app to the dynamic API no longer work. Instead, they are passed to my CatchAllController as shown in the image.

routes.MapRoute(
                "CatchAll",
                "{*urlString}",
                new { controller = "CatchAllController", action = "Index" }
            );

This is a fairly major issue for us at the moment so any help on how to resolve the problem would be appreciated.

Perhaps it has to do with moving the order of precedence for dynamic api routes above my catch all route? The catch all route should be the very last option if no other routes were found.

Regards, Sean


3 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    This is actually not related to Asp.Net Zero I think but I will try the same scenario and get back to you today. Maybe we can figure out something.

  • User Avatar
    0
    rickwheeler created

    Hi,

    It is related.

    It is to do with the way you are registering the routes and the order that they are registered in.

    The problem is that by adding a catch all route inside of the RouteConfig.cs of the web project. It gets registered BEFORE the dynamic API routes.

    Here is how I resolved the issue.

    Step 1: Add a catch all route in the PostInitialize method. This will ensure that it is registered AFTER all others.

    public override void PostInitialize()
            {
                var server = HttpContext.Current.Server;
                var appFolders = IocManager.Resolve<AppFolders>();
    
                appFolders.SampleProfileImagesFolder = server.MapPath("~/Common/Images/SampleProfilePics");
                appFolders.TempFileDownloadFolder = server.MapPath("~/Temp/Downloads");
                appFolders.WebLogsFolder = server.MapPath("~/App_Data/Logs");
                
                try { DirectoryHelper.CreateIfNotExists(appFolders.TempFileDownloadFolder); } catch { }
    
                // Register catch all here
                RouteTable.Routes.MapRoute(
                    "CatchAll",
                    "{*urlString}",
                    new { controller = "CatchAll", action = "Index" }
                );
            }
    

    Step 2: Update RouteConfig.cs with the following. We need to comment out the default route, map attribute routes and add explicit routes for scripts and views controllers

    public static class RouteConfig
        {
            public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
                // Map attribute routes
                routes.MapMvcAttributeRoutes();
    
                //ASP.NET Web API Route Config
                routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                    );
    
                routes.MapRoute(
                    "Home",
                    "",
                    new { controller = "Page", action = "Home" }
                );
    
                // Add explicit routes for some ABP controllers that were being handled by the now commented out default route
                routes.MapRoute(
                    "AbpScripts",
                    "AbpScripts/{action}",
                    defaults: new { controller = "AbpScripts" },
                    namespaces: new [] { "Abp.Web.Mvc.Controllers" }
                );
    
                routes.MapRoute(
                    "AbpAppView",
                    "AbpAppView/{action}",
                    defaults: new { controller = "AbpAppView" },
                    namespaces: new[] { "Abp.Web.Mvc.Controllers" }
                );
    
                // Comment out old default route in order to define explicit attribute routes
                //routes.MapRoute(
                //    name: "Default",
                //    url: "{controller}/{action}/{id}",
                //    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                //    namespaces: new[] { "ProductsOnline.Web.Controllers" }
                //);
                
            }
        }
    

    Step 3: Go through all Controllers and define explicit attribute routes as follows

    [AbpMvcAuthorize]
        // Add explicit attribute routing
        [Route("profile/{action}/{id?}")]
        public class ProfileController : ProductsOnlineControllerBase
        {
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi Rick,

    I didn't have time to try this one yesterday. Thank you very much for finding and sharing the solution.