複数のコントローラー アクションの開始時に実行される一般的なコードがいくつかあります。そのコードを静的クラスにリファクタリングして、そのコード ブロックの再利用を促進したいと考えています。
コードは変数をチェックし、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);
}
}
}
}
}