Angular $locationProvider with ASP.NET MVC routing

后端 未结 2 1775
小鲜肉
小鲜肉 2021-01-27 16:57

I\'m handling routing using ASP.NET MVC (using RouteCollection class). But my front end is written in angular and at some places I want to change url using Angular\'s $location

2条回答
  •  独厮守ぢ
    2021-01-27 17:28

    I don't know so much about ASP.Net. but the same problem I solved with node js like this.

    I solved the same problem with push API enable to the server. Basically angular render the page at client side and find the exact route by #. you can enable html5 mode to remove #.

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

    Remember don't forget to enable push API support at server side. just by adding

        //To suport push API 
        // Just for DEMO in your case it may be different
    app.use('/admin/*', function(req,res){
      var user = req.session.user;
       res.render("adminView",{user:user});
    });
    

    Here when you hard refresh or enter direct url into browser without # tag. server will render the home page(index) page and load your all required file. after that angular will handle all the routing for you because you have enabled html5 mode so no more need to add # in url.

提交回复
热议问题