Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8734807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:55:11+00:00 2026-06-13T09:55:11+00:00

I have some common code that runs at the beginning of multiple controller actions.

  • 0

I have some common code that runs at the beginning of multiple controller actions. I would like to refactor that code into a static class to promote reuse of that code block.

The code checks for a variable, looks for a cookie and if a condition is met, the code should redirect to another page (controller/action).

The problem is that everything works properly (including the cookie lookup) but the Redirect does not fire. The code passes over the redirect and the redirect never happens.

What is the proper way to redirect in a helper class?

Here is the code now:

This line is not working: 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);
                }
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:55:12+00:00Added an answer on June 13, 2026 at 9:55 am

    You can create an attribute that is a filter and decorate the action with it:
    (I have placed the code you provided in the attribute)

    public class SessionValidationAttribute : FilterAttribute, IAuthorizationFilter
    {
        public void OnAuthorization(AuthorizationContext filterContext)
        {
           if (filterContext.Result == null)
           {
              var id = filterContext.RouteData.Values["id"] as string;
              if (id == null || id == "")
              {
                // check cookie
                if (filterContext.Controller.ControllerContext
                   .HttpContext.Request.Cookies.AllKeys
                   .Contains("cookiename"))
                {
                    // if a session cookie was found,
                    // send to the registration recovery page
                    string sessionGuidCookieValue = "";
                    sessionGuidCookieValue = filterContext.Controller
                        .ControllerContext.HttpContext.Request
                        .Cookies["cookiename"].Value;
    
                    // check if GUID/SessionID exists in persistent cache
    
                    // send to Session Recovery
                    string redirectURL = @"~/SessionRecovery/Index/"
                            + sessionGuidCookieValue;
    
                    // this code isn't working
                    filterContext.Result = new RedirectResult(redirectURL);
                }
              }
           }
        }
    
        public abstract bool CanAccessResource(User user);
    }
    

    And on your action you do this:

    [SessionValidationAttribute]
    public ActionResult MyAction()
    {
         // code of the action
    }
    

    Or if you want to apply it to all actions inside a class:

    [SessionValidationAttribute]
    public class MyController : Controller
    {
         // code of the class, containing all actions
    }
    

    Or if you want to apply this GLOBALLY (be carefull with this):
    In your Application class (the one inheriting System.Web.HttpApplication, you can do this:

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            GlobalFilters.Filters.Add(new SessionValidationAttribute());
    
            // register routes
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some common code that I would like to share between pages and
Quick question about include/requre_once . I have some code that is common to a
I have Spring web application. I would like to put some common piece of
I have some common set up code that I've factored out to a method
I have some code that looks like this that works just fine: var info
I have three JUnit test classes that all had some common code, including identical
I have some code that prints out databse values into a repeater control on
I have some child processes which should write some logs into a common file.
Assume some domain and view objects (that have no common base class) public class
At which point do you decide that some of your subroutines and common code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.