首页 > 解决方案 > 如何将 Web 表单 Response.redirect 与 Angular 6 路由一起使用?

问题描述

我在 Web 表单项目的母版页中添加了一个 Angular 6 应用程序。

<app-root></app-root>
<script type="text/javascript" src="inline.bundle.js"></script>
<script type="text/javascript" src="polyfills.bundle.js"></script>
<script type="text/javascript" src="main.bundle.js"></script>

在 Web 表单母版页中,我有一个导航栏,其中包含单击以导航到其他 .aspx 页面的链接。在母版页 .cs 文件中,每个链接都有一个单击处理程序。

link.Click += navPrimary_Click;

protected void navPrimary_Click(object sender, EventArgs e)
    {
        Session.Add(SelectedNavItemKeyPrimary, ((LinkButton)sender).Text);
        Session.Add(SelectedNavItemKeySecondary, string.Empty);
        Response.Redirect(((LinkButton)sender).CommandArgument);
    }

角度路线如下所示:

const routes: Routes = [{ path: 'home', component: HomeComponent }];

test.aspx 页面在浏览器中加载良好(我们称之为“ http://localhost/xyz/abc/test.aspx ”)。我可以单击导航栏中的链接,浏览器会正确加载其他 .aspx 页面。在 test.aspx 页面上,有这个角度代码:

<a routerLink="/home">Angular Home</a>

当我单击“Angular Home”链接时,角度视图显示在 test.aspx 页面上,浏览器中的 url 地址现在显示“ http://localhost/home ”而不是“ http://localhost/xyz/ ” abc/test.aspx "。

现在,当我单击任何导航栏链接时,我会得到以下信息:

“/”应用程序中的服务器错误。

无法找到该资源。

说明:HTTP 404。您要查找的资源(或其依赖项之一)可能已被删除、名称已更改或暂时不可用。请查看以下 URL 并确保其拼写正确。

请求的 URL:/test.aspx

浏览器中的 url 地址现在显示“ http://localhost/test.aspx ”而不是“ http://localhost/home ”。

标签: asp.netangularwebformsangular2-routingurl-routing

解决方案


推荐阅读