为什么HttpContext.Current在asp.net mvc中的用户定义类中始终为null? [英] Why HttpContext.Current always null in user defined class in asp.net mvc ?

查看:65
本文介绍了为什么HttpContext.Current在asp.net mvc中的用户定义类中始终为null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  public   string  ProcessLongRunningAction(IEnumerable< HttpPostedFileBase>文件,字符串 id)
{
< span class =code-keyword> int currentIndex = 0 ;
foreach (HttpPostedFileBase文件 in files)
{
currentIndex ++;
lock (syncRoot)
{
string 路径= HttpContext.Current.Server.MapPath( 〜// Content // images // + file .FileName); // 此处创建了Excecption ........
file.SaveAs(path);
}
Thread.Sleep( 300 );
}
return id;
}



从控制器调用此方法,并使用要保存在images目录中的文件列表。每当执行HttpContext.Current.Server.MapPath(〜// Content // images //+ file.FileName)时,抛出NullReferenceException,并且HttpContext.Current始终为null。当我使用session时会发生同样的事情。我不知道我有什么不对劲。我马上需要帮助。请告诉我该怎么做,示例代码更明显。

感谢.......提前

解决方案

First确保你在指定的路径中有图像文件..

并尝试这样的事情,



 Server.MapPath( @ 〜/ Content / images / + file.FileName); 


I've created a class named "MyLongRunningClass" that contain a method bellow:

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;
        }


From the controller this method is called with list of file to save in images directory. whenever HttpContext.Current.Server.MapPath("~//Content//images//"+file.FileName) is executed NullReferenceException thrown, and HttpContext.Current is always null. Same thing happen when I use session. I don't know whats wrong with me. I need help Immediately. please tell me what should i do, sample code is more appreciable.
THANKS.......IN ADVANCE

解决方案

First make sure, you have image file in the specified path..
And try something like this,

Server.MapPath(@"~/Content/images/"+file.FileName);


这篇关于为什么HttpContext.Current在asp.net mvc中的用户定义类中始终为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆