我正在构建一个使用Lucene.Net进行搜索查询的ASP.NET MVC站点。我在asked a question here上了解了如何在ASP.NET MVC应用程序中正确构造Lucene.Net的使用,并被告知最好的方法是将my IndexWriter
声明为public static
,这样它就可以被重用。
下面是我的SearchController顶部的一些代码:
public static string IndexLocation = Server.MapPath("~/lucene");
public static Lucene.Net.Analysis.Standard.StandardAnalyzer analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer();
public static IndexWriter writer = new IndexWriter(IndexLocation,analyzer);
因为writer
是静态的,所以IndexLocation
也必须是静态的。因此,对于Server.MapPath()
,编译器会给出以下错误
非静态字段、方法或属性“”System.Web.Mvc.Controller.Server.get“”需要对象引用
在静态字段中,有没有使用Server.MapPath()或类似方法的方法?如何修复此错误?
发布于 2010-09-26 07:58:26
尝试使用static
的HostingEnvironment.MapPath
。
查看此SO问题以确认HostingEnvironment.MapPath
返回的值与Server.MapPath
相同:What is the difference between Server.MapPath and HostingEnvironment.MapPath?
发布于 2013-10-26 22:22:39
我想你可以试着在课堂上打进来
System.Web.HttpContext.Current.Server.MapPath("~/SignatureImages/");
*-抱歉我疏忽了,因为静态函数已经用漂移的方式回答了这个问题*
System.Web.Hosting.HostingEnvironment.MapPath("~/SignatureImages/");
更新
使用System.Web.Hosting.HostingEnvironment.MapPath("~/SignatureImages/");
时出现异常
示例详细信息: System.ArgumentException:此处不允许使用相对虚拟路径'SignatureImages‘。在System.Web.VirtualPath.FailIfRelativePath()
解决方案(在静态webmethod中测试)
System.Web.HttpContext.Current.Server.MapPath("~/SignatureImages/");
Worked
https://stackoverflow.com/questions/3795986
复制相似问题