简体   繁体   English

Angular $ locationProvider与ASP.NET MVC路由

[英]Angular $locationProvider with ASP.NET MVC routing

I'm handling routing using ASP.NET MVC (using RouteCollection class). 我正在使用ASP.NET MVC处理路由(使用RouteCollection类)。 But my front end is written in angular and at some places I want to change url using Angular's $location and I want it to support HTML5, so I added this line to my module.config : 但我的前端是用角度写的,在某些地方我想用Angular的$ location更改url,我希望它支持HTML5,所以我把这行添加到我的module.config:

$locationProvider.html5Mode(true);

But since then it seems that my routing has been handled by Angular. 但从那时起,似乎我的路由已由Angular处理。

For example my SPA is on mysite.com/user and there I want to change url using Angular's $location (so when for example users click tab url changes to mysite.com/user/tab without reloading). 例如我的SPA在mysite.com/user上,我希望使用Angular的$ location更改URL(例如,当用户单击tab url更改为mysite.com/user/tab而不重新加载时)。

But when user navigates from that page to any other (for example mysite.com/other) I want that handled by ASP.NET MVC's routing. 但是当用户从该页面导航到任何其他页面(例如mysite.com/other)时,我希望由ASP.NET MVC的路由处理。

What now is happening is that my url changes to mysite.com/other but website doesn't navigate to that page, ie MVC routing doesn't handle change. 现在发生的是我的url更改为mysite.com/other,但网站没有导航到该页面,即MVC路由不处理更改。

EDIT 编辑

I don't have any routes defined in Angular, all my routes are defined on server side and server side routing just stooped working after I added $locationProvider.html5Mode(true); 我没有在Angular中定义任何路由,我添加$locationProvider.html5Mode(true);后,我的所有路由都在服务器端定义,服务器端路由刚刚工作$locationProvider.html5Mode(true);

No unfortunately not. 不幸的是没有。 If your angular SPA is hosted at foo.com with HTML 5 mode on, foo.com/spaRoute1 will hit your ASP.Net server and expect to find that route (likely controller=SpaRoute1 action=Index). 如果您的角度SPA在foo.com上托管并foo.com了HTML 5模式, foo.com foo.com/spaRoute1将命中您的ASP.Net服务器并期望找到该路由(可能是controller = SpaRoute1 action = Index)。

You will need to set your default route to always resolve to the controller/action that is responsible for serving up your SPA. 您需要将默认路由设置为始终解析为负责提供SPA的控制器/操作。 All while defining any other routes that you need which are not specific to your SPA. 同时定义您需要的任何其他路线,这些路线并非特定于您的SPA。

I don't know so much about ASP.Net. 我对ASP.Net了解不多。 but the same problem I solved with node js like this. 但是我用这样的节点js解决了同样的问题。

I solved the same problem with push API enable to the server. 我通过推送API启用服务器解决了同样的问题。 Basically angular render the page at client side and find the exact route by # . 基本上角度在客户端渲染页面并通过#找到确切的路径。 you can enable html5 mode to remove # . 你可以启用html5模式删除#

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

Remember don't forget to enable push API support at server side. 请记住,不要忘记在服务器端启用推送API支持。 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. 这里当您在没有#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. 之后,angular将为您处理所有路由,因为您已启用html5模式,因此不再需要在url中添加#。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM