【发布时间】:2012-10-27 07:31:48
【问题描述】:
我有一些在多个控制器动作开始时运行的通用代码。我想将该代码重构为一个静态类,以促进该代码块的重用。
代码检查变量,查找 cookie,如果满足条件,代码应重定向到另一个页面(控制器/操作)。
问题是一切正常(包括 cookie 查找),但重定向没有触发。代码越过重定向,重定向永远不会发生。
在帮助类中重定向的正确方法是什么?
现在是代码:
此行不起作用:myController.HttpContext.Response.Redirect(redirectURL);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MyProject.WebUI
{
public static class SessionValidationHelper
{
// helper class to encapsulate common routines to check for missing session data
public static void SessionIdRequired(string id, Controller myController)
{
if (id == null || id == "")
{
// check cookie
if (myController.ControllerContext.HttpContext.Request.Cookies.AllKeys.Contains("cookiename"))
{
// if a session cookie was found, send to the registration recovery page
string sessionGuidCookieValue = "";
sessionGuidCookieValue = myController.ControllerContext.HttpContext.Request.Cookies["cookiename"].Value;
// check if GUID/SessionID exists in persistent cache
// send to Session Recovery
//myController.HttpContext.Response.RedirectToRoute("SessionRecovery", new { Controller = "SessionRecovery", Action = "Index", id = sessionGuidCookieValue });
string redirectURL = @"~/SessionRecovery/Index/" + sessionGuidCookieValue;
// this code isn't working
myController.HttpContext.Response.Redirect(redirectURL);
}
}
}
}
}
【问题讨论】:
标签: c# asp.net-mvc