Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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();
        }
    }
}
like image 865
blue Avatar asked Sep 24 '13 08:09

blue


2 Answers

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.

like image 92
Dennis Avatar answered Oct 02 '22 15:10

Dennis


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()

like image 23
Mohammed Dawood Ansari Avatar answered Oct 02 '22 16:10

Mohammed Dawood Ansari