使用AngularJS SPA的服务器端路由 [英] Server-side routing with an AngularJS SPA

查看:98
本文介绍了使用AngularJS SPA的服务器端路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET MVC,IIS 8,而我正在使用AngularJS创建SPA。我只在我的MVC应用程序中定义了这个路由规则,它指向 Home 控制器中的 Index 方法,其中我的SPA生活:

I'm using ASP.NET MVC, IIS 8 and I'm using AngularJS to create a SPA. I only have this routing rule defined in my MVC application, that directs to Index method in Home controller, where my SPA lives:

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

现在我有一堆客户端SPA路线,我希望所有这些路线都可以在没有hashbang的情况下使用前缀#/ 。 html5Mode完美运行 - 但只有加载应用时。如果我复制URL,关闭窗口,打开窗口并粘贴它,我收到IIS 404错误。这是有道理的,因为路由是在服务器上完成的。

Now I have bunch of client-side SPA routes and I want all of them to available without the hashbang prefix #/. html5Mode works perfectly - but only when the app has been loaded. If I copy the URL, close the window, open the window and paste it in, I get an IIS 404 error. That makes sense because the routing is done on the server.

所以共享URL不起作用,我希望能够做到这一点。因此路由也必须在服务器端完成,但每次我更改路由(添加客户端路由)时,我都不想创建服务器端路由规则。

So sharing the URL doesn't work, which I would like to be able to do. So the routing has also to be done server-side, but everytime I change the route (add client side route), I don't want to have to create a server side routing rule.

一般可以这样做吗?就像URL重写 / * #/ * 而不重定向用户? / Static 例外。

Can this be done in general? Like something that URL rewrites /* to #/* without redirecting the user? With an exception for /Static.

推荐答案

只需将网址更改为 {*}包罗万象。像这样:

Simply change the url to "{*catchall}". Like so:

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

这篇关于使用AngularJS SPA的服务器端路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆