有没有办法可以修改我的web.config文件(仅用于本地测试),以便它实际上转到index.html(加载它)然后转到/ home / about状态?
这是我当前的web.config:
<configuration> <system.web> <compilation debug="true" targetFramework="4.5" /> <httpRuntime targetFramework="4.5" /> </system.web> </configuration>
解决方法
使用此配置,您需要来自服务器的帮助.当它收到来自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下,并且对于它找到的任何目录或文件,它按原样使用.
任何其他的,被重写为/将默认文档配置为index.html,将加载SPA.
另请注意,您需要安装URL Rewrite module for IIS(IIS Express doesn’t need the module)
另一个选项是这些轻量级HTTP服务器npm包之一.
John Papa has one: lite-server.它在引擎盖下使用BrowserSync:
BrowserSync does most of what we want in a super fast lightweight
development server. It serves the static content,detects changes,
refreshes the browser,and offers many customizations.When creating a SPA there are routes that are only known to the
browser. For example,/customer/21 may be a client side route for an
Angular app. If this route is entered manually or linked to directly
as the entry point of the Angular app (aka a deep link) the static
server will receive the request,because Angular is not loaded yet.
The server will not find a match for the route and thus return a 404.
The desired behavior in this case is to return the index.html (or
whatever starting page of the app we have defined). BrowserSync does
not automatically allow for a fallback page. But it does allow for
custom middleware. This is where lite-server steps in.lite-server is a simple customized wrapper around BrowserSync to make it easy to serve SPAs.