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 338205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:24:28+00:00 2026-05-12T10:24:28+00:00

For every page requested, I need to check a cookie or create it if

  • 0

For every page requested, I need to check a cookie or create it if it’s not there.
If the cookie is there, I need to load up some info from the DB based on the contents of that cookie.

To do this I have created a base controller called AppController that my other controllers inherit from.

then I have something like this (so that the CurrentSessionValues object is available to all my controllers):

    public MySession CurrentSessionValues;
    public ApplicationController()
    {
        if (Request.Cookies["MySiteCookie"] == null)
        {
            // create new Record in DB
            CurrentSessionValues = CreateMySession();
            HttpCookie cookie = new HttpCookie("MySiteCookie");
            cookie.Value = CurrentSessionValues.SessionID.ToString;
            Response.SetCookie(cookie);
        }
        else
        {
           // use the value in MySiteCookie to get values from the DB
           // e.g. logged in user id, cart id, etc
        }

    }

When I run this, I get this error in default.aspx:

An error occurred while creating a
controller of type
‘Mvc_Learn.Controllers.HomeController’.

If the controller doesn’t have a
controller factory, ensure that it has
a parameterless public constructor.

It breaks on Request.Cookies[“MySiteCookie”]

Should I be doing this logic in some other way or another place?

  • 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-05-12T10:24:28+00:00Added an answer on May 12, 2026 at 10:24 am

    Trick is that you don’t have context in the constructor necessarily. Rather, you should override the Initialize method:

    protected override void Initialize(System.Web.Routing.RequestContext requestContext)
    {
        // DO NOT forget to call the base
        base.Initialize(requestContext);
    
        //check request context for cookie and do your thang.
    }
    

    PS: for posterity, I should note why there is an error. The key part of the exception info is that an error took place while creating the controller, the parameterless constructor bit is a red herring in this case. The error which took place was a null reference exception to HttpContext.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to intercept every request made from a page using javascript? i.e.
I have a BasePage which inherits from System.Web.UI.Page , and every page that inherits
We need to poll a web page every 5 minutes and the web page
I want to increment a cookie value every time a page is referenced even
I need to check user authorization in every view of one of my Django
It seems redundant to have zlib compress a web page during every request. It
Internet Explorer 8 breaks what must be every 3rd page I look at. The
I am trying to autoreload my page after every 20 seconds. I am using
Every time I start Emacs I see a page of help text and a
I have a page that is hitting a webservice every 5 seconds to update

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.