AddApacheModRewrite 方法提示该文件不存在(当它存在时)

AddApacheModRewrite method suggesting that file doesn't exist (when it does)

只是试图在我的 .NET Core 3.1 Web 应用程序中调用 ApacheModRewrite,但无论我做什么,它一直告诉我该文件不存在。我已经验证该文件存在于那个位置,我已经尝试创建一个自定义的 PhysicalFileProvider,并且我已经尝试将文件设置为 Content/Copy Always,但无论我做什么,我都无法通过这个代码:

 var options = new RewriteOptions()
          .AddApacheModRewrite(env.ContentRootFileProvider, env.ContentRootPath + ".htaccess");
       

错误:

System.IO.FileNotFoundException: 'The file F:\Source\MyWebsite\htaccess.txt does not exist.'

其中 env.ContentRootFileProvider 解析为路径:“F:\Source\MyWebsite” 我再次确认 .htaccess 文件确实存在。我也尝试过各种不同的方法来访问 AddApacheModRewrite 中的文件路径,但我真的在这个问题上摸不着头脑。

我做错了什么?

想通了。我必须使用流阅读器,读取文件,然后才能应用它:

   using (StreamReader apacheModRewriteStreamReader 
           File.OpenText(env.ContentRootPath + "\.htaccess")) 
   {                    
        var options = new RewriteOptions()
                          .AddApacheModRewrite(apacheModRewriteStreamReader);
                    
        app.UseRewriter(options);    
   }