blue
blue

Reputation: 588

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

Upvotes: 8

Views: 56242

Answers (2)

Dennis
Dennis

Reputation: 14495

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.

Upvotes: 24

Mohammed Dawood Ansari
Mohammed Dawood Ansari

Reputation: 885

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

Upvotes: 2

Related Questions