ASP.NET MVC:在MVC中重定向到页面/操作的正确方法是什么? [英] ASP.NET MVC: What is the correct way to redirect to pages/actions in MVC?

查看:243
本文介绍了ASP.NET MVC:在MVC中重定向到页面/操作的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对MVC还是很陌生,但不确定到底是哪个重定向...替代了WebForms中使用的标准重定向,即标准Response.Redirect()

I am fairly new to MVC but not sure exactly which Redirect... replaces the standard redirect used in WebForms ie the standard Response.Redirect()

例如,在两种情况下,我需要重定向到其他页面:

For instance, I need to redirect to other pages in a couple of scenarios:

1)如果用户注销(表单注销"在操作中),我想重定向到登录页面.

1) WHen the user logs out (Forms signout in Action) I want to redirect to a login page.

2)在控制器或基本控制器事件(例如Initialize)中,我想重定向到另一个页面(AbsoluteRootUrl +控制器+动作)

2) In a Controller or base Controller event eg Initialize, I want to redirect to another page (AbsoluteRootUrl + Controller + Action)

似乎在某些情况下会调用多个重定向,这会导致错误,这与页面已被重定向的事实有关吗?如何取消当前请求并仅重定向?

It seems that multiple redirects get called in some cases which causes errors, something to do with the fact a page is already being redirected? How can cancel the current request and just redirect?

更新:

此问题的答案( System.Web.Mvc.Controller初始化)指示不应该使用Initialize而应该使用OnActionExecuting?

The answer to this question (System.Web.Mvc.Controller Initialize) indicates that Initialize should not be used and OnActionExecuting should be used?

关于为什么不应该使用Initialize或为什么OnAuthorization不是更好的选择的任何评论吗?

Any comments on why Initialize should not be used or why OnAuthorization is not a better option?

更多信息:

此博客文章( http://blog.wekeroad .com/blog/aspnet-mvc-securing-your-controller-actions/)表明OnActionExecuting对于身份验证很有用(如上面的链接所示),我想将它添加到基本Controller中的该事件中该类很好,因为每个页面都在MVC中运行一个Action,因此不应有太大的区别,并且具有重定向的功能应该更容易.这确实是有道理的,但对我来说似乎也有意义,事情可以在此事件发生之前的某个事件中完成,并且使我们怀疑这些事件的用途是什么?会给OnActionExecuting一个机会.

This blog post (http://blog.wekeroad.com/blog/aspnet-mvc-securing-your-controller-actions/) indicates that OnActionExecuting is useful for authentication (as indicated in the link above) I guess adding this to that event in the base Controller class is fine as every page runs an Action in MVC so shouldnt make much difference and having the ability to redirect should be easier. This does make sense, but it also seems to make sense to me that things could be done in an event before this event and makes we question what those events are for? Will be giving OnActionExecuting a go..

推荐答案

1)当用户注销(表单退出操作")时,我想重定向到登录页面.

1) When the user logs out (Forms signout in Action) I want to redirect to a login page.

public ActionResult Logout() {
    //log out the user
    return RedirectToAction("Login");
}

2)在Controller或基本Controller事件(例如Initialze)中,我想重定向到另一个页面(AbsoluteRootUrl + Controller + Action)

2) In a Controller or base Controller event eg Initialze, I want to redirect to another page (AbsoluteRootUrl + Controller + Action)

为什么要从控制器init重定向?

Why would you want to redirect from a controller init?

如果您想从控制器上的index操作重定向,路由引擎会自动处理传入的请求,只需执行以下操作:

the routing engine automatically handles requests that come in, if you mean you want to redirect from the index action on a controller simply do:

public ActionResult Index() {
    return RedirectToAction("whateverAction", "whateverController");
}

这篇关于ASP.NET MVC:在MVC中重定向到页面/操作的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆