首页 文章

使用Asp.net 4.5进行Angular2路由

提问于
浏览
1

我需要帮助 . 我开始用Asp.net核心开发angular2,一切都很完美 . 为了将所有404请求重定向到index.html,我在Startup类中使用此代码 .

app.Use(async (context, next) =>
        {
            await next();

            if (context.Response.StatusCode == 404
                && !Path.HasExtension(context.Request.Path.Value))
            {
                context.Request.Path = "/index.html";
                await next();
            }
        });

但是,我需要返回到asp.net 4.5,现在我遇到了一个很大的路由问题 . 我尝试在Owin Startup类中使用类似的代码,但这并没有解决问题 .

如何将所有请求移动到index.html?一个例子:我有重定向到/ logout的链接,但现在使用此代码,app.module看不到/注销,我被重定向到应用程序的主页 .

在教程中如何在最后一段中使用angular2与asp 4.5(https://angular.io/docs/ts/latest/cookbook/visual-studio-2015.html)说:

如果此应用程序使用Angular路由器,则浏览器刷新可能会返回404 - 未找到页面 . 看看地址栏 . 它是否包含导航网址(“深层链接”)...除/或/index.html以外的任何路径?您必须配置服务器以返回index.html以获取这些请求 . 在此之前,请删除导航路径并再次刷新 .

这该怎么做?谢谢!

1 回答

  • 2

    你可以将所有内容重定向到index.html页面来解决这个问题,这是我在我的一个项目中使用的配置代码:

    <rewrite>
      <rules>
        <rule name="RedirectEverything" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_URI}" matchType="Pattern" pattern="^/api/" negate="true" />
          </conditions>
          <action type="Rewrite" url="/index.html" />
        </rule>
      </rules>
    </rewrite>
    

    在web.config中的 system.websever 部分下添加此项

相关问题