【问题标题】:The problem with setting up IIS UrlRewrite rules for ASP.NET Core 3.1 after migration迁移后为 ASP.NET Core 3.1 设置 IIS UrlRewrite 规则的问题
【发布时间】:2021-04-21 07:12:48
【问题描述】:

我正在尝试将 ASP.NET Core Web 应用程序从 2.2 迁移到 3.1。该应用程序的前端应用程序是Angular。这是来自Startup.cs 的工作asp.net core 2.2 代码:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    //...

    app.UseCors(x => x.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
    
    app.UseAuthentication();
    ConfigureUrlRewriting(app, env);
    ConfigureRouting(app);
}

private static void ConfigureUrlRewriting(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment() && File.Exists("IISUrlRewrite.xml"))
    {
        using (var reader = File.OpenText("IISUrlRewrite.xml"))
        {
            var options = new RewriteOptions().AddIISUrlRewrite(reader);
            app.UseRewriter(options);
        }
    }
}

private static void ConfigureRouting(IApplicationBuilder app)
{
    app.UseDefaultFiles(new DefaultFilesOptions
    {
        DefaultFileNames = new List<string> { "index.html" }
    });
    app.UseStaticFiles();
    app.UseMvc(routes =>
    {
        routes.MapRoute(name: "default", template: "api/{controller}/{id}");
    });
}

迁移到 asp.net core 3.1 following the guide from Microsoft docs 后,代码更改如下:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    //...

    app.UseCors(x => x.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
    
    app.UseAuthentication();
    ConfigureUrlRewriting(app, env);
    ConfigureRouting(app);
}


private static void ConfigureUrlRewriting(IApplicationBuilder app, IHostEnvironment env)
{
    if (env.IsDevelopment() && File.Exists("IISUrlRewrite.xml"))
    {
        using (var reader = File.OpenText("IISUrlRewrite.xml"))
        {
            var options = new RewriteOptions().AddIISUrlRewrite(reader);
            app.UseRewriter(options);
        }
    }
}

private static void ConfigureRouting(IApplicationBuilder app)
{
    app.UseDefaultFiles(new DefaultFilesOptions
    {
        DefaultFileNames = new List<string> { "index.html" }
    });
    app.UseStaticFiles();

    // Changed only the lines below
    app.UseRouting();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(name: "default", pattern: "api/{controller}/{id}");
    });
}

在IIS本地服务器上部署后,出现所有请求静态文件返回index.html的问题,证明如下:

这是IISUrlRewrite.xml的内容:

<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>

似乎条件&lt;add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /&gt; 与静态文件不匹配。但是当应用程序是asp.net core 2.2时,条件是数学的

有人可以解释一下迁移后的这种行为吗?是.net core代码的问题还是IIS端的问题?

【问题讨论】:

  • 您可以启用fail request tracing 来检查请求。如果规则匹配成功并且 url 被重写,则问题可能是由 url 重写引起的。然后尝试禁用规则并再次请求。
  • @BruceZhang 谢谢你的回复。我找到了解决问题的方法。我稍后会添加问题的答案
  • @BruceZhang 不幸的是,我没有找到解决方案。布鲁斯,我已经尝试了你的步骤,看到像localhost/filename.js 这样的每个请求都转换为localhost(主页)。似乎没有通过输入{REQUEST_FILENAME} 的条件。我还检查了api 请求,并查看它们的条件是否有效
  • URL 成功匹配重写规则,显示索引页。所以你可以改变规则的条件。我不知道您的具体需求和初始网址,无法提供有用的建议。
  • @BruceZhang 我有一个有角度的单页应用程序。所以除了api,文件和目录请求之外的所有请求都应该使用中间件重定向到index.html。根据有关 Angular 的 url 重写的其他问题,此 url 重写规则应该有效。但正如你所看到的,它不是

标签: c# asp.net-core iis url-rewriting migration


【解决方案1】:

哇,我找到了问题的答案。引用Microsoft docs about Url Rewrite Middleware:

中间件不支持这些模块的全部功能。

服务器模块的某些功能不适用于 ASP.NET 核心项目,如 IsFile 和 IsDirectory 约束 IIS 重写模块。在这些情况下,请改用中间件。

ASP.NET Core 似乎不支持 MatchType IsFileIsDirectory。 因此,如果您的 IIS UrlRewrite 规则包含使用 MatchType IsFileIsDirectory 的条件,您应该直接使用 IIS Url Rewrite 模块设置 url 重写

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-27
    • 2011-05-06
    • 2020-05-28
    • 2021-01-15
    • 2020-05-27
    相关资源
    最近更新 更多