tags:

views:

21

answers:

1

Hi I want all my pages to use have some viewdata. i need to get a client name from a querystring then based on that do some work and populate ViewData. my controller inherits from the controller created below. Request["client"] is giving System.NullReferenceException: Object reference not set to an instance of an object.

 public abstract class ApplicationController : Controller
    {

        public ApplicationController()
        {
            string client = Request["client"];
            //...etc

        }

    }

what is wrong with this?

thanks

A: 

What is wrong is that the request does not exist yet when you call it in the constructor is only gets instantiated when an action is invoked on your method.

See this question and look at the answer. Maybe it is usefull for you too

http://stackoverflow.com/questions/2503032/where-to-use-controller-httpcontext/2503085#2503085

Chino