簡體   English   中英

路由IIS-Angular-Asp.Net核心

[英]Routing IIS - Angular - Asp.Net Core

我發布此消息是因為找不到任何可行的答案。 我正在使用Angular 6,Asp.net Core 2.2和IIS 10開發網站。我的問題涉及在瀏覽器URL輸入區域中直接輸入地址時的路由。 使用該網站,在內部導航中,一切正常。 我的網站計划是:

/ 
--> /upload
--> /admin
----> /admin/admincreateuser
----> /admin/manageuserlicencepanel

這是我的設置:

角度6

export const appRoutes: Routes = [
  {
    path: 'upload',
    runGuardsAndResolvers: 'always',
    canActivate: [AuthGuard],
    children: [
        {path: '', component: UploadComponent, data: {roles: ['Customer']} },
    ]
  },
  {
    path: 'admin',
    runGuardsAndResolvers: 'always',
    canActivate: [AuthGuard],
    children: [
        {path: '', component: AdminComponent, data: {roles: ['Admin', 'Moderator']}},
        {path: 'admincreateuser', component: AdminCreateUserComponent, data: {roles: ['Admin', 'Moderator']}},
        {path: 'manageuserlicencepanel', component: ManageUserLicencePanelComponent, data: {roles: ['Admin', 'Moderator']}}}}
    ]
  },
  {path: '', component: HomeComponent},
  {path: '**', redirectTo: '', pathMatch: 'full'}
];

ASP.Net Core 2.2

Startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseMvc(routes =>
    {              
        routes.MapSpaFallbackRoute(
                    name: "spa-fallback",
                    defaults: new { controller="FallbackController", action = "Index"}
        );
    }
}

FallbackController.cs

public class FallbackController : Controller
{
    public IActionResult Index()
    {
        return PhysicalFile(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "index.html"), "text/HTML");
    }
}

即使在IIS部署上,此配置也可以正常工作。 但是無法在地址欄中直接輸入網址(即https://www.dzfzx.com/upload ),因為我收到404錯誤。 為了解決這個問題,我試圖像這樣在IIS中添加URL重寫

   <system.webServer>
        <rewrite>
            <rewriteMaps>
              <rewriteMap name="^(.*)$" />
            </rewriteMaps>
            <rules>
                <rule name="All routes" stopProcessing="true">
                    <match url="^(.*)$" />
                    <action type="Rewrite" url="/" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_URI}" pattern="/api(.*)$" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_URI}" pattern="^api/" negate="true" /> 
                    </conditions>
                </rule>
            </rules>
        </rewrite>
    </system.webServer>

通過實現,我直接在主頁上看到一個錯誤:

未捕獲到的SyntaxError:意外令牌<

還要注意,似乎我從來沒有在FallbackController中回退,這似乎是不正常的。

您是否知道如何使它起作用(直接地址欄輸入)?

謝謝。

看來您的配置不正確。 您應該將所有請求定向到index.html ,這是angular的索引文件。

試試這個配置:

<?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="./index.html" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>

</configuration>

將Angular應用程序部署到IIS

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM