简体   繁体   English

Angular 8 - 路由在本地工作,但在生产环境中部署时,路由不起作用 - .NET 核心 C# 后端

[英]Angular 8 - routing works locally, but when deployed on Production, routes don't work - .NET Core C# backend

In my app I created routes, they work perfectly on localhost, I can navigate to:在我的应用程序中,我创建了路线,它们在 localhost 上完美运行,我可以导航到:

localhost:4200/about

but when deployed on Production, if I navigate to:但是当部署在生产上时,如果我导航到:

myapp.com/about

what will happen is, the browser would almost instantly rewrite the URL to: myapp.com/index.html and then right afterwards the final url I will see displayed in the navigation bar of the browser, will be the default path: "myapp.com/"将会发生的事情是,浏览器几乎会立即将 URL 重写为: myapp.com/index.html ,然后紧接着最终的 url 将在浏览器的导航栏中显示: com/"

My routing code is:我的路由代码是:

const appRoutes: Routes = [
  { path: "", redirectTo: "/", pathMatch: "full" },
  { path: "about", component: AboutComponent },
  { path: "**", redirectTo: "/" },
];

@NgModule({
  imports: [
      RouterModule.forRoot(appRoutes)
  ],
  exports: [
      RouterModule
  ]
})
export class AppRoutingModule {}

and I app module I also added, as I read it could help:我还添加了我的应用程序模块,因为我读到它可能会有所帮助:

providers: [{provide: LocationStrategy, useClass: PathLocationStrategy}] , providers: [{provide: LocationStrategy, useClass: PathLocationStrategy}] ,

on the backEnd side (frontend and backend are in the same project) this is the code:在后端(前端和后端在同一个项目中)这是代码:

public void Configure(
....
            app.UseDefaultFiles();
            app.UseSpaStaticFiles();
            app.UseMvc(routes => {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
                routes.MapSpaFallbackRoute("spa-fallback", new { controller = "Home", action = "Index" 
            });
            });
           app.UseSpa(spa => {
                spa.Options.SourcePath = "FrontEnd";
                if (env.IsDev()) {
                    spa.UseAngularCliServer(npmScript: "start");
                }
            });

the HomeController : HomeController

  public IActionResult Index() => Redirect("index.html");

the web.config file: web.config文件:

<configuration>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
    </handlers>
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
  </system.webServer>
</configuration>

So basically whenever deployed, if I try to navigate to my route " /about ", it will just rewrite to index.html and then redirect to "/"所以基本上无论何时部署,如果我尝试导航到我的路线“ /about ”,它只会重写为index.html然后重定向到“/”

Any ideas?有任何想法吗?

This works for me but on an IIS server.这对我有用,但在 IIS 服务器上。 Create a web.config file and place it in the root folder of Angular dist files.创建一个 web.config 文件并将其放在 Angular dist 文件的根文件夹中。

    <?xml version="1.0" encoding="utf-8"?>
       <configuration>
         <system.webServer>
           <rewrite>
           <rules>
            <rule name="Angular Routes" stopProcessing="true">
              <match url=".*" />
               <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />   
             </conditions>
             <action type="Rewrite" url="/frontend/" />//Folder with my Angular files
          </rule>
         </rules>
       </rewrite>
         </system.webServer>
      </configuration>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET Core(HttpSys)路由在本地工作,但在部署时不工作 - ASP.NET Core (HttpSys) Routing works locally but not when deployed 仅在 UAT web 服务器中部署时出现路由错误 404。 在当地工作。 ASP.NET核心 - Routing error 404 only when deployed in UAT web server. Works locally. ASP.NET Core C# Azure ServiceBus / Azure SQL - 在本地工作,部署到 Azure 时似乎不起作用 - C# Azure ServiceBus / Azure SQL - works locally, does not seem to work when deployed to Azure 引用COM对象的ASP.Net Core 2.0在本地工作,但在部署时无法工作 - ASP.Net Core 2.0 referencing COM objects works locally but not when deployed C#更新和数据库集(datediff)在C#中无效,但在SQL Server中有效 - C# update and set of database (datediff) don't work when in C# but works if in SQL Server ASP.NET核心集成测试在本地工作,但在生产环境中运行时会抛出空引用异常 - ASP.NET Core Integration Test works locally but throws null reference exception when running in production environment C#预处理器在本地工作,但在部署时不工作 - C# Preprocessor working locally but not when deployed 带有Selenium的C#.NET-MVC应用程序在服务器上部署时不起作用 - C# .NET - MVC app with Selenium doesn't work when deployed on Server .Net Core WebApp API 端点一旦部署到 Azure 就不起作用 - .Net Core WebApp API Endpoints don't work once deployed to Azure 诊断部署到生产环境C#.NET的.NET代码中的问题 - Diagnose issues in .NET code deployed to a production environment, C# .NET
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM