RouteConfig triggers 500 error when refreshing page

前端 未结 1 1803
灰色年华
灰色年华 2020-12-06 15:24

I am using angularJS with ASP.NET. When I run my Web application, all links are working and there is no 500 error.

But when I refresh page I got 500 e

相关标签:
1条回答
  • 2020-12-06 15:42

    The issue is related to improperly set server side. Firstly try to turn off the html5 mode

    //$locationProvider.html5Mode(true);
    $locationProvider.html5Mode({enabled: false});
    

    And check that after refresh all is working. It should.

    Change the routing like this:

    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.IgnoreRoute("fonts*.woff");
    routes.IgnoreRoute("*.js");
    routes.IgnoreRoute("*.html");
    routes.IgnoreRoute("*.css");
    routes.IgnoreRoute("api/*");
    
    routes.MapRoute(
        name: "Default",
        url: "{dummyController}/{dummyAction}/{dummy1}/{dummy2}/{dummy3}",
        defaults: new { controller = "Home", action = "Index"
            , dummyController = UrlParameter.Optional
            , dummyAction = UrlParameter.Optional
            , dummy1 = UrlParameter.Optional
            , dummy2 = UrlParameter.Optional
            , dummy3 = UrlParameter.Optional
        }
    );
    

    This should do what we need. Whenever there is anything comming to server:

    • it ends with js, html, css ... it is returned
    • /api/ (ASP.NET Web API) is also skipped here
    • and any url like /somestuff/somepart is treated as Home/Index

    This setting url: "{dummyController}/{dummyAction}/{id}", makes the above. any part coming from html5mode is treated as a route key "dummyController", "dummyAction", while the Home and Index are passed as controller and action in the defaults: new { controller = "Home", action = "Index" ...

    And because your application is expecting some parts of your routing, you should use it like this:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.IgnoreRoute("fonts*.woff");
        routes.IgnoreRoute("*.js");
        routes.IgnoreRoute("*.html");
        routes.IgnoreRoute("*.css");
        routes.IgnoreRoute("api/*");
    
        // keep this application special settings
        routes.MapRoute("templates", "templates/{action}.html",
         new { controller = "Home", action = "Templates", name = "" }
        );
        routes.MapRoute("contacts","contacts",
           new { controller = "Home", action = "Contacts", id = UrlParameter.Optional }
        );
    
        // this should do the job on F5
        routes.MapRoute(
            name: "Default",
            url: "{dummyController}/{dummyAction}/{dummy1}/{dummy2}/{dummy3}",
            defaults: new { controller = "Home", action = "Index"
                , dummyController = UrlParameter.Optional
                , dummyAction = UrlParameter.Optional
                , dummy1 = UrlParameter.Optional
                , dummy2 = UrlParameter.Optional
                , dummy3 = UrlParameter.Optional
            }
    
    );
    

    Also check this

    How to: Configure your server to work with html5Mode

    0 讨论(0)
提交回复
热议问题