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.
我建立一个ASP。使用Lucene的MVC网站。网络搜索查询。我在这里问了一个关于如何正确构造Lucene的问题。Net在ASP中的使用。NET MVC应用程序,并被告知最好的方法是将我的IndexWriter声明为public static,这样它就可以被重用。
Here is some code that is at the top of my SearchController:
下面是我的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);
As writer
is static, IndexLocation
must also be static. Thus, the compiler is giving me the following error for Server.MapPath()
:
由于writer是静态的,IndexLocation也必须是静态的。因此,编译器给了我以下服务器. mappath()的错误:
An object reference is required for the non-static field, method, or property 'System.Web.Mvc.Controller.Server.get'
非静态字段、方法或属性的System.Web.Mvc.Controller.Server.get需要对象引用。
Is there a way of using Server.MapPath() or something similar from a static field? How can I fix this error?
是否有使用Server.MapPath()或类似于静态字段的方法?我如何修正这个错误?
2 个解决方案
#1
209
Try HostingEnvironment.MapPath
, which is static
.
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?
请参见这个SO问题,以确认HostingEnvironment。MapPath返回与服务器相同的值。MapPath:服务器之间的区别是什么?MapPath HostingEnvironment.MapPath ?
#2
33
I think you can try this for calling in from a class
我想你可以试着从课堂上打电话进来
System.Web.HttpContext.Current.Server.MapPath("~/SignatureImages/");
*----------------Sorry I oversight, for static function already answered the question by adrift*
*---------------- ---------------- ---------------- -------------
System.Web.Hosting.HostingEnvironment.MapPath("~/SignatureImages/");
Update
更新
I got exception while using System.Web.Hosting.HostingEnvironment.MapPath("~/SignatureImages/");
使用system . web . host.hostingenvironment . mappath(“~/SignatureImages/”)时出现异常;
Ex details : System.ArgumentException: The relative virtual path 'SignatureImages' is not allowed here. at System.Web.VirtualPath.FailIfRelativePath()
交货的细节:系统。ArgumentException:此处不允许使用相对虚拟路径“SignatureImages”。在System.Web.VirtualPath.FailIfRelativePath()
Solution (tested in static webmethod)
解决方案(在静态webmethod中测试)
System.Web.HttpContext.Current.Server.MapPath("~/SignatureImages/");
Worked
System.Web.HttpContext.Current.Server.MapPath(~ / SignatureImages / ");工作
#1
209
Try HostingEnvironment.MapPath
, which is static
.
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?
请参见这个SO问题,以确认HostingEnvironment。MapPath返回与服务器相同的值。MapPath:服务器之间的区别是什么?MapPath HostingEnvironment.MapPath ?
#2
33
I think you can try this for calling in from a class
我想你可以试着从课堂上打电话进来
System.Web.HttpContext.Current.Server.MapPath("~/SignatureImages/");
*----------------Sorry I oversight, for static function already answered the question by adrift*
*---------------- ---------------- ---------------- -------------
System.Web.Hosting.HostingEnvironment.MapPath("~/SignatureImages/");
Update
更新
I got exception while using System.Web.Hosting.HostingEnvironment.MapPath("~/SignatureImages/");
使用system . web . host.hostingenvironment . mappath(“~/SignatureImages/”)时出现异常;
Ex details : System.ArgumentException: The relative virtual path 'SignatureImages' is not allowed here. at System.Web.VirtualPath.FailIfRelativePath()
交货的细节:系统。ArgumentException:此处不允许使用相对虚拟路径“SignatureImages”。在System.Web.VirtualPath.FailIfRelativePath()
Solution (tested in static webmethod)
解决方案(在静态webmethod中测试)
System.Web.HttpContext.Current.Server.MapPath("~/SignatureImages/");
Worked
System.Web.HttpContext.Current.Server.MapPath(~ / SignatureImages / ");工作