我正在构建一个 ASP.NET MVC 站点,我在其中使用 Lucene.Net 进行搜索查询。我 asked a question here关于如何在 ASP.NET MVC 应用程序中正确构建 Lucene.Net 用法,并被告知最好的方法是将我的 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()
提供了以下错误:
An object reference is required for the non-static field, method, or property 'System.Web.Mvc.Controller.Server.get'
是否有从静态字段使用 Server.MapPath() 或类似方法的方法?我该如何解决这个错误?
最佳答案
尝试 HostingEnvironment.MapPath
,这是 static
。
查看此 SO 问题以确认 HostingEnvironment.MapPath
返回与 Server.MapPath
相同的值:What is the difference between Server.MapPath and HostingEnvironment.MapPath?
关于c# - 在 ASP.NET MVC 中的静态字段中使用 Server.MapPath(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3795986/