【问题标题】:Redirect from a webform to MVC index view从 Web 表单重定向到 MVC 索引视图
【发布时间】:2019-12-17 14:41:45
【问题描述】:

我有一个 MVC 应用程序托管多个 SSRS 报告,每个报告在一个单独的 WebForm 上。这些报告都在 Home 文件夹中的 Index View 上捆绑在一起,我在其中获得了 href 链接以将用户定向到他们想要的报告。

现在的问题是我试图在这些 WebForms 上创建一个按钮,它将用户重定向回 Index 视图。我在 Stack Overflow 上搜索了指导,并提出了多个已检查的答案并试图尝试一下。但是,他们似乎都没有将我重定向到所需的页面。相反,他们只是将我重定向到我所在的同一页面。

我尝试了以下解决方案:

在我看来,问题在于我的路由,但是,我对路由 URL 的了解还不够,无法解决这个问题。

对于上下文,这是我第一次编写 C# MVC 应用程序。我已经并且目前正在网络上阅读有关最佳实践等的文档和代码 sn-ps,但是由于我的同事没有提供可用的指导,并且经验不足 2 周,因此很难理解该做什么和不该做什么。非常感谢任何帮助和提示!

重定向的示例是从 UserManagement.aspx (~/WebForms/IT/UserManagement.aspx) 页面到 Index.cshtml (~/Views/Home/Index.cshtml) 页面

这是我目前拥有的:

My RouteCongif.cs

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = 
                    UrlParameter.Optional });
        }
UserManagement.aspx


<div>
    <asp:Button ID="BackButton" runat="server" Text="Back" />
</div>

The BackButton_Click function in UserManagement.aspx.cs (CodeBehind)

protected void BackButton_Click(object sender, EventArgs e)
        {
            UrlHelper urlHelp = new 
            UrlHelper(HttpContext.Current.Request.RequestContext);
            Response.Redirect(urlHelp.Action("Index", "Home"), false);
        }

【问题讨论】:

  • 您尝试过 RedirectToAction 吗? docs.microsoft.com/en-us/previous-versions/aspnet/…
  • 我尝试了 Response.Redirect,因为我使用的是 WebForm。即使我提到了 using system.web.mvc,RedirectToAction 也会给我在当前上下文中不存在的错误。如果我没记错的话,RedirectToAction 是在 cshtml 页面中使用的。

标签: c# asp.net asp.net-mvc webforms response.redirect


【解决方案1】:

在控制器中,您可以使用 RedirectToActionReturn View("View Name")。 从视图中将 javascript 写为 window.location.href=/Controller/View 点击按钮

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-26
    • 1970-01-01
    • 2019-10-02
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    • 1970-01-01
    相关资源
    最近更新 更多