You must Sign In to post a response.
  • Category: [Competition Entries]

    MVC Controller - HTTPContext is NULL

    Hi ,

    I am trying to access 'httpcontext' inside of a method in controller1.

    Actually I am calling this method from another controller2 by creating through object(controller1).But httpcontext is null, please throw some light here.

    public class Controller1{
    //My actions go here

    public void custommethod()
    var'some value here');


    //Here my another controller class
    public class Controller2{
    Controller1 obj= new Controller1();

  • #757511
    You can not access httpcontext out side of action result method. if you are calling controller2, then you can access httpcontext only here. you can use the parameters to send to controller1 or send return parameters from controller1 to controller2
    Saurabh Tyagi

    Rate As Answer, If you satisfied.

  • #757779
    You Call Controller like
    var httpException = exception as HttpException;
    var routeData = new RouteData();
    routeData.Values["controller"] = "Errors";
    routeData.Values["action"] = "General";
    routeData.Values["exception"] = exception;
    Response.StatusCode = 500;

    Response.StatusCode = httpException.GetHttpCode();
    switch (Response.StatusCode)
    case 403:
    routeData.Values["action"] = "PageForBidden"; break;
    case 404:
    routeData.Values["action"] = "PageNotFound"; break;

    IController errorsController = new ErrorController();
    var rc = new RequestContext(new HttpContextWrapper(Context), routeData);

    Navaneetha krishnan
    Be a man of Action.Don't be man of words.

  • Sign In to post your comments