asp.net-mvc ASPX 页面响应。重定向到 MVC 视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25672952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:13:10  来源:igfitidea点击:

ASPX Page Response.Redirect to MVC View

asp.net-mvcasp.net-mvc-5

提问by MizAkita

Is it possible to Response.Redirect in aspx that returns a view from MVC?

是否可以在从 MVC 返回视图的 aspx 中进行 Response.Redirect?

I have an login.aspx page that i need upon successful login to redirect to a view in ~/Views/Home/index.cshtml is this possible? I've tried to following but the url couldnt be resolved.

我有一个 login.aspx 页面,我需要在成功登录后重定向到 ~/Views/Home/index.cshtml 中的视图,这可能吗?我试过,但网址无法解析。

tried:

试过:

  1. Response.Redirect("~/Views/Home/Index.cshtml");

  2. Response.Redirect("Home");

  1. Response.Redirect("~/Views/Home/Index.cshtml");

  2. Response.Redirect("Home");

The controller is "HomeController" and the view is "Index.cshtml" any help with how to call a controller and view or atleast just the url would help.

控制器是“HomeController”,视图是“Index.cshtml”,任何有关如何调用控制器和视图的帮助或至少只有 url 会有所帮助。

回答by Dimitar Dimitrov

How about Response.Redirect("/Home/Index");- that is of course presuming that you have a HomeControllerwith an Indexaction that returns the view you're looking for (by convention it's going to be a view having the same name as the action, unless you specify otherwise).

怎么样Response.Redirect("/Home/Index");- 这当然是假设你有一个返回你正在寻找的视图HomeControllerIndex动作(按照惯例,它将是一个与动作同名的视图,除非你另外指定)。

回答by MikeSmithDev

Here is how I redirect from .aspxto an MVC view:

这是我如何从.aspxMVC 视图重定向:

var page = HttpContext.Current.Handler as Page;
Response.Redirect(page.GetRouteUrl("yourRouteNameOrDefault", 
    new { Controller="Home", Action="Index"}), false);

Keeps you from hard-coding in the actual path and instead uses your routing and also allows you to build in routeParameters.

防止您在实际路径中进行硬编码,而是使用您的路由,并且还允许您在routeParameters.

回答by Scott Selby

try this:

尝试这个:

return RedirectToAction("Index");

assuming that this is in the same controller , if not:

假设这是在同一个控制器中,如果不是:

return RedirectToAction("Index", "Home");

in the MVC framework you don't redirect to views directly , you redirect to Actions, then there me be some logic in there , and based on the logic from the Action it will choose a view and fill out a view model if necessary

在 MVC 框架中,您不直接重定向到视图,而是重定向到 Actions,然后在那里有一些逻辑,并且根据 Action 中的逻辑,它将选择一个视图并在必要时填写一个视图模型

回答by DinoMyte

Try this :

尝试这个 :

Response.Redirect(Url.Action("Index", "Home"));

回答by Aminur Rahman

Try This

尝试这个

Response.Redirect("~/Home/Index");

回答by Hemang Padia

var page = HttpContext.Current.Handler as Page;
Response.Redirect(page.GetRouteUrl("Default",
    new { Controller = "Nameofcontroller", Action = "Nameofaction" }), false);