【发布时间】:2019-12-17 14:41:45
【问题描述】:
我有一个 MVC 应用程序托管多个 SSRS 报告,每个报告在一个单独的 WebForm 上。这些报告都在 Home 文件夹中的 Index View 上捆绑在一起,我在其中获得了 href 链接以将用户定向到他们想要的报告。
现在的问题是我试图在这些 WebForms 上创建一个按钮,它将用户重定向回 Index 视图。我在 Stack Overflow 上搜索了指导,并提出了多个已检查的答案并试图尝试一下。但是,他们似乎都没有将我重定向到所需的页面。相反,他们只是将我重定向到我所在的同一页面。
我尝试了以下解决方案:
- Redirecting from asp.net web form to mvc 4
- After add MapPageRoute to an asp.net mvc project, the site stops to enter in Home Controller
- How to Redirect From WebForms to a MVC View?
- Redirect from webform to MVC ..getting errors
在我看来,问题在于我的路由,但是,我对路由 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