c# - 在 ASP.NET MVC 中的静态字段中使用 Server.MapPath()

标签 c# asp.net-mvc static lucene.net server.mappath

我正在构建一个 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/

相关文章:

c# - Unity - 在没有 resolve() 的情况下解析

c# - 如何动态覆盖 Html.ActionLink 生成的 URL?

c# - LINQ to Entities 不支持指定的类型成员 'UserName'

c# - Asp.Net/C#静态变量持久化

c++ - C++中的静态常量 double

swift - 访问子类时不能在类型错误的实例上使用静态成员

c# - Unity5 - 如何限制游戏对象的旋转

c# - 将 HttpClient 类与 WinRT 结合使用

asp.net-mvc - 如何从 Controller 获取所有 Action 名称

c# - Bitmap.LockBits "pin"位图是否存入内存?