[英]How to configure Angular 6 URL rewrite in dotnet core using IIS appsettings.json
I'm deploying an Angular SPA to my .NET Core app using IIS. 我正在使用IIS将Angular SPA部署到我的.NET Core应用程序。
I do a production Angular build (ng build --base-href /ng --deploy-url /ng) and deploy it to wwwroot in the ng directory. 我做了一个生产Angular构建(ng build --base-href / ng --deploy-url / ng)并将其部署到ng目录中的wwwroot。
I currently have it working when you manually go to the Angular url for index.html: 当你手动转到index.html的Angular网址时,我目前正在使用它:
https://localhost:44316/ng/index.html
However, if you refresh or manually navigate to a url, you get a 404. 但是,如果您刷新或手动导航到网址,则会获得404。
https://localhost:44316/ng/dash
Not surprising, given that Angular says this will happen without a URL redirect. 并不奇怪,因为Angular说这将在没有URL重定向的情况下发生。 In the Angular documentation , it says to insert a rewrite rule into web.config. 在Angular文档中 ,它说要在web.config中插入重写规则。
However, I'm using .NET Core, and one of my team members has informed me that modifying the web.config XML is the old way of doing things. 但是,我正在使用.NET Core,我的一个团队成员告诉我,修改web.config XML是一种旧的做事方式。 It looks like I need to modify appsettings.json to make this rewrite rule happen. 看起来我需要修改appsettings.json才能使这个重写规则发生。
Soo....Anyone done this before? Soo ....之前有人这么做过吗? If I don't modify web.config to set up my forward, how do I set it up? 如果我不修改web.config来设置我的转发,我该如何设置它?
Have you tried using hash strategy to confirm that this may be a server misconfiguration? 您是否尝试过使用哈希策略来确认这可能是服务器配置错误? Usually 404 happens when the server is not pointing to the "main angular's page", which is the index.html you mentioned. 通常404当服务器没有指向“主角度页面”时发生,这是你提到的index.html。
To use hash strategy you just need to add this parameter to your RouterModule import: 要使用哈希策略,您只需将此参数添加到RouterModule导入:
RouterModule.forRoot(routes, {useHash: true}) RouterModule.forRoot(routes,{useHash:true})
I forget where I found this little code snippet, but it did the trick. 我忘记了我发现这个小代码片段的位置,但它确实做到了。 I used the following in Configure() in Startup.cs: 我在Startup.cs中的Configure()中使用了以下内容:
app.Run(async (context) =>
{
context.Response.ContentType = "text/html";
await context.Response.SendFileAsync(Path.Combine(env.WebRootPath, "ng/index.html"));
});
Basically, on all the pages in my ng directory, it will load up index.html (without changing the url). 基本上,在我的ng目录中的所有页面上,它将加载index.html(不更改URL)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.