request querystring from abstract controller
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 abstr开发者_JAVA百科act class ApplicationController : Controller
{
public ApplicationController()
{
string client = Request["client"];
//...etc
}
}
what is wrong with this?
thanks
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
Where to use Controller.HttpContext
精彩评论