问题描述
我建设我使用Lucene.Net搜索查询,其中一个ASP.NET MVC的网站。我如何正确结构Lucene.Net使用在ASP.NET MVC应用程序,被告知最好的方法是申报我的的IndexWriter
作为一个问题公共静态
,因此,它可被重新使用。
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.
下面是一些code那是在我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);
由于作家
是静态的, 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?
的先谢谢了。的
推荐答案
尝试<$c$c>HostingEnvironment.MapPath$c$c>,这是静态
。
请参阅确认这太问题的 HostingEnvironment.MapPath
返回相同的值使用Server.Mappath
:What是使用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?
这篇关于使用使用Server.Mappath()在ASP.NET MVC中静态字段内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!