![](/img/trans.png)
[英]An object reference is required for the non-static field, method, or property 'System.Web.UI.Page.Request.get'
[英]An object reference is required for the non-static field, method, or property 'System.Web.Mvc.Controller.HttpContext.get'
我在我的MVC3應用程序中收到此錯誤。 請幫忙...
錯誤:
An object reference is required for the non-static field, method, or property 'System.Web.Mvc.Controller.HttpContext.get'
在線:
string desigId = HttpContext.Current.Session["Desig_Id"].ToString();
類中的代碼及其方法:
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();
}
}
}
您的基類Controller
已經實現了屬性HttpContext
。
你可以完全引用它: System.Web.HttpContext.Current...
或者使用你的控制器的屬性,就像HttpContext.Session
一樣。 對於第二個選項,您的方法必須non-static
。
此錯誤的另一個原因是您不能在靜態方法中使用HttpContext.Current.Session
和Server.MapPath()
在這種情況下,您可以使用HostingEnvironment.MapPath()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.