5

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

2 に答える 2

22

基本クラスControllerはすでにプロパティを実装していますHttpContext

完全に修飾されたものを参照するかSystem.Web.HttpContext.Current...、コントローラーのプロパティを使用することができますHttpContext.Session。2 番目のオプションの場合、方法は適切でなければなりませんnon-static

于 2013-09-24T09:02:13.817 に答える
2

エラーのもう 1 つの理由は、静的メソッドでHttpContext.Current.Sessionandを使用できないことです。Server.MapPath()

そのような場合に利用できますHostingEnvironment.MapPath()

于 2016-01-02T12:45:58.440 に答える