问题描述
我正在构建一个 ASP.NET MVC 站点,我在其中使用 Lucene.Net 进行搜索查询.我在这里问了一个问题 关于如何在 ASP.NET MVC 应用程序中正确构建 Lucene.Net 用法,并被告知最好的方法是将我的 IndexWriter
声明为 public static
,以便它可以重复使用.
I'm building an ASP.NET MVC site where I'm using Lucene.Net for search queries. I asked a question here about how to properly structure Lucene.Net usage in an ASP.NET MVC application and was told that the best method is to declare the my IndexWriter
as public static
, so that it can be re-used.
这是我的 SearchController 顶部的一些代码:
Here is some code that is at the top of my 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()
错误:
As writer
is static, IndexLocation
must also be static. Thus, the compiler is giving me the following error for Server.MapPath()
:
非静态字段、方法或属性System.Web.Mvc.Controller.Server.get"需要对象引用
是否有使用 Server.MapPath() 或来自静态字段的类似方法?如何修复此错误?
Is there a way of using Server.MapPath() or something similar from a static field? How can I fix this error?
推荐答案
尝试 HostingEnvironment.MapPath
,这是static
.
请参阅此 SO 问题以确认 HostingEnvironment.MapPath
返回与 Server.MapPath
相同的值:Server.MapPath 和 HostingEnvironment.MapPath 有什么区别?
See this SO question for confirmation that HostingEnvironment.MapPath
returns the same value as Server.MapPath
: What is the difference between Server.MapPath and HostingEnvironment.MapPath?
这篇关于在 ASP.NET MVC 的静态字段中使用 Server.MapPath()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!