首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ASP.net OWIN AngularJS html5mode url重定向

ASP.net OWIN AngularJS html5mode url重定向
EN

Stack Overflow用户
提问于 2015-09-03 23:42:35
回答 2查看 662关注 0票数 1

我正在尝试用Web后端来建立一个单一的页面应用程序。我使用"OWIN WebAPI SPA模板“来启动我的项目。此模板默认为解决方案根目录中的公用/文件夹服务的静态文件。我想支持html5网址,IE。localhost/madeUpPath,它应该访问index.html页面。为了做到这一点,我在Web.config文件中设置了一个重写规则:

代码语言:javascript
复制
<system.webServer>
  <rewrite>
      <rules>
          <rule name="Main Rule" stopProcessing="true">
              <match url=".*" />
              <conditions logicalGrouping="MatchAll">
                  <add input="{REQUEST_URI}" pattern="api/" ignoreCase="true" negate="true" />

                  <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                  <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
              </conditions>
              <action type="Rewrite" url="/" />
          </rule>
      </rules>
  </rewrite>
</system.webServer>

我遇到的问题是,由于静态文件驻留在/public/ REQUEST_FILENAME}中,{}认为该文件相对于/,而url localhost/main.css被重写为localhost/

我试着把这个规则改为:

代码语言:javascript
复制
                  <add input="public/{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                  <add input="public/{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />

但这不起作用。我怎样才能达到我想要的结果?谢谢。

编辑:,我发现了一些似乎有用的东西,但并不是我想要的。

代码语言:javascript
复制
<rewrite>
    <rules>
        <rule name="Main Rule" stopProcessing="true">
            <match url="([a-zA-Z0-9-_.\/]+\.(css|js|less|json|jpeg|jpg|png|gif|svg|xml|html))$" />
            <action type="Rewrite" url="/public/{R:0}" />
        </rule>
        <rule name="Second Rule" 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="/" />
        </rule>
    </rules>
</rewrite>
EN

回答 2

Stack Overflow用户

发布于 2015-09-29 06:05:21

首先,这段代码并不是因为您使用OWIN服务器来处理静态文件:

代码语言:javascript
复制
           <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        </conditions>

请您使用下面的代码:

代码语言:javascript
复制
<rewrite>
    <rules>
        <rule name="Main Rule" stopProcessing="true">
            <match url="^((?!(api|\.)).)*$" />
            <action type="Rewrite" url="/" />
        </rule>
    </rules>
</rewrite>

正则表达式使我们能够忽略api和任何文件路径。

票数 1
EN

Stack Overflow用户

发布于 2018-04-24 13:44:39

我们还使用Owin为子文件夹中的内容提供服务,以下内容非常适合我们:

代码语言:javascript
复制
<rewrite>
  <rules>
    <rule name="Angular Routes" stopProcessing="true">
      <match url=".*" />
      <conditions logicalGrouping="MatchAll">
        <add input="{APPL_PHYSICAL_PATH}public{URL}" matchType="IsFile" ignoreCase="true" negate="true" />
        <add input="{APPL_PHYSICAL_PATH}public{URL}" matchType="IsDirectory" ignoreCase="true" negate="true" />
        <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
      </conditions>
      <action type="Rewrite" url="/" />
    </rule>
  </rules>
</rewrite>

它非常类似于这句话,但我将它们的{REQUEST_URI}变量更改为{URL},这样即使它们有查询字符串,也会忽略现有文件,而查询字符串对于缓存破坏资源(如site.css?ec99043d9af49f2bd7c2 )是必要的。

编辑:我也刚刚找到了此Microsoft示例,经过一些调整可能也适用于这个场景,但我还没有亲自测试过它:

代码语言:javascript
复制
  <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="/MyApp/" />
      <!--<action type="Rewrite" url="/" />-->
      </rule>
    </rules>
  </rewrite>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32387089

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档