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();
}
}
}
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With