无法添加对System.Web.Hosting的引用

时间:2014-10-07 13:04:06

标签: c#

我正在将Web应用程序从VB迁移到C#。我还在VS2013中升级到了Update 3。 Hosting班有变化吗?我使用Hosting.HostingEnvironment.MapPath收到错误,我甚至无法添加对System.Web.Hosting的引用,因为它无处可寻。当我尝试在添加引用时搜索程序集时,使用整个名称空间即System.Web.Hosting,它不返回任何结果。

我在类中使用了using语句,并且它没有变灰,这意味着它被用于某些东西,但是代码不喜欢Hosting中的Hosting.HostingEnvironment,因为它是眩目的红色。我甚至没有在intellisense中获得Hosting类,该项目引用了System.Web

5 个答案:

答案 0 :(得分:9)

没有Hosting类。相反,您需要HostingEnvironment类:

HostingEnvironment.MapPath("~/Hello.txt");

完整类型HostingEnvironmentSystem.Web.Hosting.HostingEnvironment,因此您需要在文件中包含using System.Web.Hosting;子句,或使用全名。

更重要的是,如果您正在制作网络应用程序,那么您很可能不想使用HostingEnvironment。你应该总是有一个例如HttpContextPage / Control,可让您访问Server.MapPath,这应该是首选。

至于引用,System.Web.Hosting命名空间位于System.Web.dll,所以只要确保你有一个参考,你应该没事。

由于您要从VB迁移它,我认为冲突是由VB对命名空间的不同处理引起的。在C#中,你不能这样做:

using System.Web;

Hosting.HostingEnvironment.DoWhatever();

使用命名空间时,要么使用包含命名空间的完整类型名称,要么在确切的命名空间和类型上使用using。将两者结合起来并不是很有效。

答案 1 :(得分:3)

我有同样的问题,不是发布的内容。我在一个单独的(模型)项目中查看库中的代码。在我使用的库项目中,没有引用System.Web。

棘手的部分是引用了System.Web.Http,因此找到了System.Web命名空间,因此语句using System.Web;编译得很好。

为自己省去一些麻烦和心痛,请按解决方案资源管理器中的“与活动文档同步”按钮,如本答案所示。 https://stackoverflow.com/a/30517179/149884

答案 2 :(得分:1)

您是否尝试使用System.Web中的Server.MapPath

答案 3 :(得分:1)

I don't think you can use the .NET Client Profile to build Web Applications?

Anyway, that aside. You find that namespaces tend to be re-used across different assemblies. Especially in the framework assemblies where they believe some classes conceptually belong together even if they support different technologies.

I am not sure I explained that well but take this example. There is a System namespace in mscorlib, System, System.Net, System.Core and System.Numerics. Also, System.Web may show up in System.Web.Http, or System.Web itself, and others like System.Web.Abstractions, System.Web.Optimization, etc. As a result just trying to use a using statement to discern the assembly a particular class came from can really throw you off.

The typical classes in the System.Web.Hosting namespace are in the framework assembly System.Web.dll. Microsoft has been trying to de-emphasize the direct use of System.Web.dll in favor of the more modular implementation of Katana/Kestrel.

Having said that, make sure your project directly references System.Web.dll. To use the required class either refer to it by its complete name ie System.Web.Hosting.HostingEnvironment. Or put a using System.Web.Hosting; at the beginning of your .cs file.

It is possible to have a property in the current class that is named HostingEnvironment, or a class from another namespace in another assembly that is named HostingEnvironment. In that instance, you may need to specify the class name in full or come up with a moniker for easy reference and to reduce typing.

For instance, you could have this at the beginning of your file:

using HostEnv = System.Web.Hosting.HostingEnvironment;

Then somewhere in the body of your code, you could make reference to it thus:

var appHost = HostEnv.ApplicationHost;

Does this help?

For the particular scenario you want to address, you may then do this:

var resolvedPath = HostEnv.MapPath(pathToMap);

答案 4 :(得分:-1)

从客户端更改您的图书馆资料。

Changing client profile