小编Suz*_*que的帖子

为什么HttpContext.Current在asp.net mvc中的用户定义类中始终为null?

我创建了一个名为MyLongRunningClass包含方法的类:

 public string ProcessLongRunningAction(IEnumerable<HttpPostedFileBase> files ,string id)
    {
        int currentIndex = 0;

        foreach (HttpPostedFileBase file in files)
        {
            currentIndex++;
            lock(syncRoot)
            {                
             string path=HttpContext.Current.Server.MapPath("~//Content//images       //"+file.FileName);//Excecption is created here........
              file.SaveAs(path);
            }
          Thread.Sleep(300);
        }
        return id;
    }
Run Code Online (Sandbox Code Playgroud)

从控制器调用此方法,使用文件列表保存在images目录中.无论何时HttpContext.Current.Server.MapPath("~//Content//images//"+file.FileName)被执行NullReferenceException抛出,HttpContext.Current总是如此null.当我使用session时会发生同样的事情.我不知道代码有什么问题.

asp.net-mvc

6
推荐指数
1
解决办法
1万
查看次数

标签 统计

asp.net-mvc ×1