Перенаправление к указанному контроллеру и действию в asp.net mvc фильтр действия

Вы никогда не должны серьезно считать размещение PHP в HTML. Вы смешиваете логику с представлением, которое делает проект грязным.

, Поскольку другие сказали, если Вы производите в HTML, используйте <?php echo $whatever; ?>

, Если необходимо произвести тонну информации, изучают выходную буферизацию.

43
задан Nick Larsen 24 April 2012 в 13:35
поделиться

2 ответа

EDIT: The original question was about how to detect session logout, and then automatically redirect to a specified controller and action. The question proved much more useful as it's current form however.


I ended up using a combination of items to achieve this goal.

First is the session expire filter found here. Then I wanted someway to specify the controller/action combo to get a redirect URL, which I found plenty of examples of here. In the end I came up with this:

public class SessionExpireFilterAttribute : ActionFilterAttribute
{
    public String RedirectController { get; set; }
    public String RedirectAction { get; set; }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        HttpContext ctx = HttpContext.Current;

        if (ctx.Session != null)
        {
            if (ctx.Session.IsNewSession)
            {
                string sessionCookie = ctx.Request.Headers["Cookie"];
                if ((null != sessionCookie) && (sessionCookie.IndexOf("ASP.NET_SessionId") >= 0))
                {
                    UrlHelper helper = new UrlHelper(filterContext.RequestContext);
                    String url = helper.Action(this.RedirectAction, this.RedirectController);
                    ctx.Response.Redirect(url);
                }
            }
        }

        base.OnActionExecuting(filterContext);
    }
}
17
ответ дан 26 November 2019 в 22:36
поделиться

Call RedirectToAction , используя эту перегрузку :

protected internal RedirectToRouteResult RedirectToAction(
    string actionName,
    RouteValueDictionary routeValues
)

В фильтрах действий история немного другая. Хороший пример см. Здесь:

http://www.dotnetspider.com/resources/29440-ASP-NET-MVC-Action-filters.aspx

5
ответ дан 26 November 2019 в 22:36
поделиться
Другие вопросы по тегам:

Похожие вопросы: