简体   繁体   English

如何使用IIS appsettings.json在dotnet核心中配置Angular 6 URL重写

[英]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.

相关问题 如何在 .NET Core 的 appSettings.json 上配置 IdentiyServer 设置? - How to configure IdentiyServer settings on appSettings.json of .NET Core? 如何使用angular2从appsettings.json读取typescript文件的值? - How to read values from appsettings.json to typescript file using angular2? 如何在 Angular 10 中读取 appsettings.json 文件配置 - How to read appsettings.json file configuration in Angular 10 如何使用angular4从appsettings.json到打字稿文件中读取键值 - How to read key values from appsettings.json to typescript file using angular4 尝试在 appSettings.json - Angular 和 Typescript 中设置环境变量 - Trying to set environment variables in appSettings.json - Angular and Typescript 带有Angular的.NET Core:如何配置IIS以HTML5模式进行URL重写? - .NET Core with Angular : How do I configure IIS for URL Rewriting in HTML5 mode? Angular 7 + dotnet核心+ SignalR IIS问题 - Angular 7 + dotnet core + SignalR IIS Issue 在apache部署do.net core API和angular 2 server url重写规则报错 - Deploy dotnet core API and angular in apache 2 server url rewrite rule error 从 Angular 服务中的 .NET appSettings.json 文件读取环境变量? - Read an environment variable from a .NET appSettings.json file in an Angular service? 如何配置 IIS URL 重写模块以在 url 上使用“HTML 5 pushState”样式和哈希片段? - How to configure IIS URL Rewrite module to work with "HTML 5 pushState" style and hash fragments on url?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM