Reloading the page gives wrong GET request with AngularJS HTML5 mode

前端 未结 24 3255
慢半拍i
慢半拍i 2020-11-22 01:39

I want to enable HTML5 mode for my app. I have put the following code for the configuration, as shown here:

return app.config([\'$routeProvider\',\'$location         


        
24条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 01:56

    If you are in .NET stack with MVC with AngularJS, this is what you have to do to remove the '#' from url:

    1. Set up your base href in your _Layout page:

    2. Then, add following in your angular app config : $locationProvider.html5Mode(true)

    3. Above will remove '#' from url but page refresh won't work e.g. if you are in "yoursite.com/about" page refresh will give you a 404. This is because MVC does not know about angular routing and by MVC pattern it will look for a MVC page for 'about' which does not exists in MVC routing path. Workaround for this is to send all MVC page request to a single MVC view and you can do that by adding a route that catches all url


    routes.MapRoute(
            name: "App",
            url: "{*url}",
            defaults: new { controller = "Home", action = "Index" }
        );
    

提交回复
热议问题