无法添加对System.Web.Hosting的引用 [英] can't add reference to System.Web.Hosting

查看:79
本文介绍了无法添加对System.Web.Hosting的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Web应用程序从VB迁移到C#.我还升级了VS2013中的Update 3.是否对 Hosting 类进行了更改?使用 Hosting.HostingEnvironment.MapPath 时出现错误,我什至无法添加对 System.Web.Hosting 的引用,因为找不到它.当我尝试在添加引用时使用整个名称空间(即 System.Web.Hosting )搜索程序集时,它不返回任何结果.

我在类中有using语句,并且它不会灰显,这意味着它正在用于某些东西,但是代码不喜欢 Hosting.HostingEnvironment Hosting >呈红色.我什至没有在intellisense中获得 Hosting 类,并且该项目具有对 System.Web

的引用

解决方案

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

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

HostingEnvironment 的完整类型为 System.Web.Hosting.HostingEnvironment ,因此您需要使用System.Web.Hosting; >文件中的子句,或使用全名.

但是,更重要的是,如果要制作Web应用程序,则很可能不想使用 HostingEnvironment .您应该始终有一个实例,例如 HttpContext Page / Control ,可让您访问应该首选的 Server.MapPath .

作为参考, System.Web.Hosting 命名空间位于 System.Web.dll 中,因此只需确保您对此有一个参考,就应该很好.

由于您是从VB进行迁移的,因此我认为冲突是由VB对命名空间的不同处理引起的.在C#中,您不能只是这样做:

 使用System.Web;Hosting.HostingEnvironment.DoWhatever(); 

使用名称空间时,请使用完整类型名称(包括名称空间),或在确切的名称空间和类型上使用using.将两者结合起来并不太奏效.

I am migrating a web application from VB to C#. I have also upgraded to Update 3 in VS2013. Were there changes to the Hosting class? I'm getting an error using Hosting.HostingEnvironment.MapPath and I can't even add a reference to System.Web.Hosting as it's nowhere to be found. When I try to search the assemblies when adding a reference, using the whole namespace i.e. System.Web.Hosting, it returns no result.

I have the using statement in the class and it is NOT grayed out meaning it is being used for something but yet the code doesn't like Hosting in Hosting.HostingEnvironment as it's in glaring red. I don't even get the Hosting class in intellisense and the project has a reference to System.Web

解决方案

There is no Hosting class. Instead, you want the HostingEnvironment class:

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

The full type of HostingEnvironment is System.Web.Hosting.HostingEnvironment, so you need to have a using System.Web.Hosting; clause in the file, or use the full name.

Even more importantly, though, if you're making a web application, you most likely don't want to use HostingEnvironment anyway. You should always have an instance of e.g. HttpContext or Page / Control, which give you access to Server.MapPath, which should be preferred.

As for the reference, System.Web.Hosting namespace lives in System.Web.dll, so just make sure you have a reference to that and you should be fine.

Since you're migrating this from VB, I assume that the conflict is caused by VB's different treatment of namespaces. In C#, you can't just do this:

using System.Web;

Hosting.HostingEnvironment.DoWhatever();

When using a namespace, either use the full type name including the namespace, or use a using on the exact namespace, and the type. Combining the two doesn't quite work.

这篇关于无法添加对System.Web.Hosting的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆