8

I am getting this error in my MVC3 Application. Please Help...

Error :

An object reference is required for the non-static field, method, or property 'System.Web.Mvc.Controller.HttpContext.get'

On Line:

string desigId = HttpContext.Current.Session["Desig_Id"].ToString();

the code with its method in class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Helpers;
using System.Net.Mail;
using System.Net;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading.Tasks;

namespace ApricaCRMEvent.Controllers
{
    public class NotificationController : Controller
    {
        //to send email notification
        [Authorize]
        public static string SendEmailNotification(int crmId, string username, string action)
        {

              string desigId = HttpContext.Current.Session["Desig_Id"].ToString();
        }
    }
}

2 Answers 2

24

Your base class Controller already implements a property HttpContext.

You can either reference it fully qualified: System.Web.HttpContext.Current... or use the Property of your controller, just like HttpContext.Session. For the second option, your method must be non-static though.

1
  • 1
    It would be nice if you were to link to the english version of the documentation instead of the german.
    – crthompson
    Commented Dec 12, 2016 at 23:08
2

One more reason for the error is that you can't use HttpContext.Current.Session and Server.MapPath() in static method

In such case you can make use of HostingEnvironment.MapPath()

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.