如何正确刷新在VS 2015 Web开发环境中运行的AngularJS SPA
我有一个AngularJS SPA应用程序,我使用Visual Studio 2015开发.当我点击发布它发布index.html并且工作得很好.但是,如果我在页面上并单击刷新,则会尝试刷新SPA页面,例如example.com/home/about.这失败了,因为我没有家/关于页面.
有没有办法可以修改我的web.config文件(仅用于本地测试),以便它实际上转到index.html(加载它)然后转到/ home / about状态? 这是我当前的web.config: <configuration> <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" /> </system.web> </configuration> 解决方法
您似乎正在使用
html5mode.在这种情况下,没有#来保持URL从请求更改为服务器.
使用此配置,您需要来自服务器的帮助.当它收到来自SPA路线的请求时,它将为index.html提供服务. 这个SO answer有关于在web.config上配置URL Rewrite的详细信息: 规则通过: <system.webServer> <rewrite> <rules> <rule name="AngularJS Routes" stopProcessing="true"> <match url=".*" /> <conditions logicalGrouping="MatchAll"> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" /> </conditions> <action type="Rewrite" url="/" /> </rule> </rules> </rewrite> </system.webServer> 它假定您的API位于:/ api下,并且对于它找到的任何目录或文件,它按原样使用. 另请注意,您需要安装URL Rewrite module for IIS(IIS Express doesn’t need the module) 另一个选项是这些轻量级HTTP服务器npm包之一.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |