【问题标题】:How to run AngularJs html5mode on ASP.Net Core using URL Rewrite?如何使用 URL Rewrite 在 ASP.Net Core 上运行 AngularJs html5mode?
【发布时间】:2019-08-23 10:47:05
【问题描述】:

我有一个在 .net 框架上运行的 angularjs 应用程序。当我尝试在 .net 核心上运行它时,我无法修复刷新页面时的 html5mode 后备出现错误 404

我已经尝试为 url 重写和创建中间件创建 web.config。

   <system.webServer>
    <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>
  </system.webServer>

我想使用 URL Rewrite 在 .net 核心上运行 angularjs html5mode

【问题讨论】:

    标签: angularjs asp.net-core url-rewriting html5mode


    【解决方案1】:

    在您的项目中,创建一个类似 IISUrlRewrite.xml 的 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>
    

    然后在您的 Startup.cs Configure() 上读取该文件并将其附加到 RewriteOptions().AddIISUrlRewrite

       using (StreamReader iisUrlRewriteReader = File.OpenText("IISUrlRewrite.xml"))
       {
                var rules = new RewriteOptions();
    
                rules.AddIISUrlRewrite(iisUrlRewriteReader);
    
                app.UseRewriter(rules);
       }
       app.UseFileServer();
    

    来源参考here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-12
      • 2019-09-22
      • 1970-01-01
      • 2021-05-20
      相关资源
      最近更新 更多