【问题标题】:Angular2 routing with Asp.net 4.5Angular2 路由与 Asp.net 4.5
【发布时间】:2017-04-20 03:22:07
【问题描述】:

我需要帮助。 我开始用 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看不到/logout,我被重定向到应用程序的主页。

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

如果此应用程序使用 Angular 路由器,浏览器刷新可以 返回 404 - 找不到页面。看地址栏。可以 包含导航网址(“深层链接”)...除 / 或之外的任何路径 /index.html?

您必须将服务器配置为为这些返回 index.html 要求。在您这样做之前,请移除导航路径并再次刷新。

如何做到这一点? 谢谢!

【问题讨论】:

    标签: asp.net asp.net-mvc angular angular2-routing


    【解决方案1】:

    您可以将所有内容重定向到 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 部分下添加它

    【讨论】:

    • seconded...刚刚发现这个,因为我必须将 4.5 与 Vue 应用程序一起使用,并且具有与 OP 类似的代码。这太棒了!
    猜你喜欢
    • 1970-01-01
    • 2012-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-28
    • 1970-01-01
    相关资源
    最近更新 更多