ASPX Page Response.Redirect to MVC View

2020-03-06 07:20发布

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

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.

tried:

  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.

6条回答
三岁会撩人
2楼-- · 2020-03-06 07:47

Try This

Response.Redirect("~/Home/Index");
查看更多
够拽才男人
3楼-- · 2020-03-06 07:53

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

查看更多
我欲成王,谁敢阻挡
4楼-- · 2020-03-06 08:00

How about Response.Redirect("/Home/Index"); - that is of course presuming that you have a HomeController with an Index action 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).

查看更多
何必那么认真
5楼-- · 2020-03-06 08:00
var page = HttpContext.Current.Handler as Page;
Response.Redirect(page.GetRouteUrl("Default",
    new { Controller = "Nameofcontroller", Action = "Nameofaction" }), false);
查看更多
Melony?
6楼-- · 2020-03-06 08:09

Try this :

Response.Redirect(Url.Action("Index", "Home"));
查看更多
淡お忘
7楼-- · 2020-03-06 08:14

Here is how I redirect from .aspx to an MVC view:

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.

查看更多
登录 后发表回答