1

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..

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

Comments

0

Request.ServerVariables.Get("HTTP_REMOTE_USER") won't help in .net core mvc straight away. You should read server variables when for example:

ip = Request.HttpContext.GetServerVariable("HTTP_X_FORWARDED_FOR");
ip = Request.HttpContext.GetServerVariable("REMOTE_ADDR");

See also: Access HttpContext in ASP.NET Core

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.