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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 177k
  • Answers 177k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I don't think getting mstest to behave differently (i.e. using… May 12, 2026 at 3:32 pm
  • Editorial Team
    Editorial Team added an answer MsgBox WeekdayName(Weekday(Now)) May 12, 2026 at 3:32 pm
  • Editorial Team
    Editorial Team added an answer No, unfortunately, this is an area where XSD is lacking… May 12, 2026 at 3:32 pm

Related Questions

I'm trying to figure out the best way to do caching for a website
I'm confused on ASP.NET MVC caching and authorization and in dire need of some
I'm creating a cms and have not yet settled on the matter of where
The background to this question is that I need to use some user session

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.