asp.net mvc when should i read servervariables?

Trimack picture Trimack · Jun 10, 2009 · Viewed 10.9k times · Source

When is the apropriate time (part of code) that i can gather information from servervariables? I mean, i have tried calling this

string temp = Request.ServerVariables.Get("HTTP_REMOTE_USER");

in the Controller constructor, but i am getting a nullpointerexception, while in an action it works properly. And it just doesnt seem very clean to call it in each action..

Answer

eu-ge-ne picture eu-ge-ne · Jun 10, 2009

You could define a property in your controller or Base Controller:

protected string HttpRemoteUser
{
    get
    {
        return Request.ServerVariables.Get("HTTP_REMOTE_USER");
    }
}

And then call this property in your actions instead of Request.ServerVariables.Get