簡體   English   中英

非靜態字段,方法或屬性'System.Web.Mvc.Controller.HttpContext.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.SessionServer.MapPath()

在這種情況下,您可以使用HostingEnvironment.MapPath()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM