Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试在静态方法中使用HttpContext.Session_C#_Asp.net_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

C# 尝试在静态方法中使用HttpContext.Session

C# 尝试在静态方法中使用HttpContext.Session,c#,asp.net,asp.net-mvc,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,我有以下给出错误的方法 非静态字段、方法或属性“HttpContext.Session”需要对象引用 namespace Website.Controllers { // CONTROLLER CLASS IS HERE BUT REMOVED AS NOT RELEVANT CODE public static class HtmlExtensions { public static decimal GetCartTotal() { decimal

我有以下给出错误的方法

非静态字段、方法或属性“HttpContext.Session”需要对象引用

namespace Website.Controllers
{

  // CONTROLLER CLASS IS HERE BUT REMOVED AS NOT RELEVANT CODE

  public static class HtmlExtensions
  {
    public static decimal GetCartTotal()
    {
      decimal cartTotal = 0;

      var storedData = HttpContext.Session.GetString(ShoppingCartTotal);

      if (storedData != null)
      {
        cartTotal = JsonConvert.DeserializeObject<decimal>(storedData);
      }

      return cartTotal;
    }
  }
}

您应该从调用者传递方法的依赖项,最好在方法中添加一个参数,该参数接受
会话
对象输入,调用者需要传入该参数:

public static decimal GetCartTotal(HttpSessionState session)
{
  decimal cartTotal = 0;

  var storedData = session.GetString(ShoppingCartTotal);

  if (storedData != null)
  {
    cartTotal = JsonConvert.DeserializeObject<decimal>(storedData);
  }

  return cartTotal;
}

您应该从调用者传递方法的依赖项,最好在方法中添加一个参数,该参数接受
会话
对象输入,调用者需要传入该参数:

public static decimal GetCartTotal(HttpSessionState session)
{
  decimal cartTotal = 0;

  var storedData = session.GetString(ShoppingCartTotal);

  if (storedData != null)
  {
    cartTotal = JsonConvert.DeserializeObject<decimal>(storedData);
  }

  return cartTotal;
}

HttpContext.Current是您的朋友。但是要明智地使用它,不要把它放在逻辑上不可能有HttpContext的地方。我认为,如果它是用于Razor中的消费,您也可以。另一种选择是使用所有相关控制器都从中继承的BaseController,这样您就可以将共享资源放在一个地方,供多个操作访问。HttpContext.Current是您的朋友。但是要明智地使用它,不要把它放在逻辑上不可能有HttpContext的地方。我认为,如果它打算在Razor中使用,您也可以。另一种选择是使用所有相关控制器都继承自的BaseController,这样您就可以将共享资源放在一个地方,供多个操作访问。您甚至可以将其作为扩展方法,这样您就可以执行
var ShoppingCartTotal=HttpContext.Session.GetCartTotal()从razor视图。(顺便说一句,我将传递给函数的类型修复为正确的类型)@ScottChamberlain yeah,在帖子中还补充说,您甚至可以将其作为扩展方法,这样您就可以执行
var shoppingcartotal=HttpContext.Session.getcartotal()从razor视图。(顺便说一句,我将传递给函数的类型修改为正确的类型)@ScottChamberlain yeah,在帖子中也添加了这一点
public static decimal GetCartTotal(this HttpSessionState session)
{
      decimal cartTotal = 0;

      var storedData = session.GetString(ShoppingCartTotal);

      if (storedData != null)
      {
        cartTotal = JsonConvert.DeserializeObject<decimal>(storedData);
      }

      return cartTotal;
}
var total = HttpContext.Session.GetCartTotal()