Sam*_*tar 8 visual-studio angularjs
我有一个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>
Run Code Online (Sandbox Code Playgroud)
看来你正在使用html5mode.在这种情况下,不能#
保持URL从请求到服务器.使用此配置,您需要来自服务器的帮助.当它收到来自SPA路线的请求时,它将为index.html提供服务.
这个SO答案详细介绍了如何在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>
Run Code Online (Sandbox Code Playgroud)
它假定您的API位于:/api
并且对于它找到的任何目录或文件,它按原样使用.如果将其他任何内容重写为/
已配置默认文档的内容index.html
,则会为您加载SPA.
另请注意,您需要为IIS 安装URL Rewrite模块(IIS Express不需要该模块)
另一个选项是这些轻量级HTTP服务器npm包之一. John Papa有一个:lite-server.它使用了BrowserSync:
BrowserSync在超快速轻量级开发服务器中完成了我们想要的大部分工作.它提供静态内容,检测更改,刷新浏览器,并提供许多自定义.
创建SPA时,只有浏览器知道的路由.例如,/ customer/21可以是Angular应用程序的客户端路由.如果手动输入此路由或直接链接到Angular应用程序(也称为深层链接)的入口点,则静态服务器将接收请求,因为尚未加载Angular.服务器将找不到路由的匹配项,因此返回404.在这种情况下,所需的行为是返回index.html(或我们定义的应用程序的任何起始页).BrowserSync不会自动允许回退页面.但它确实允许自定义中间件.这是lite-server的用武之地.
lite-server是一个围绕BrowserSync的简单自定义包装器,可以轻松地为SPA提供服务.
归档时间: |
|
查看次数: |
277 次 |
最近记录: |