C# HttpContext 在类库中不可用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14149815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:52:50  来源:igfitidea点击:

HttpContext not available in Class Library

c#httpcontextsystem.web

提问by Boardy

I am working on a project where I have a C# class library which needs to use the System.web.HttpContext. I've done this before in another project without problem but now its not working. I'm not sure what I am missing they're both targeting .net 3.5 and I've added the reference System.weband added the directive using System.web.

我正在开发一个项目,其中有一个 C# 类库需要使用System.web.HttpContext. 我之前在另一个项目中做过这个没有问题,但现在它不起作用。我不确定我错过了什么,他们都针对 .net 3.5,我已经添加了引用System.web并添加了指令using System.web

However, when I try and do HttpContextnothing is found. I've tried using the full path System.web.HttpContextbut the only thing that comes up are 3 items related to ASP.

但是,当我尝试什么HttpContext都不做时发现。我试过使用完整路径,System.web.HttpContext但唯一出现的是 3 个与 ASP 相关的项目。

Below are screenshots from the working project intellisense and the non-working intellisense

以下是工作项目智能感知和非工作智能感知的屏幕截图

Below is the working screenshot

下面是工作截图

working screenshot

工作截图

and below is the non working screenshot

下面是非工作截图

not working screenshot

不工作截图

Thanks for any help you can provide

感谢您的任何帮助,您可以提供

采纳答案by competent_tech

This means that either you are using a Client Profile as the Target Framework or the reference to System.Web was not added correctly.

这意味着您将客户端配置文件用作目标框架,或者未正确添加对 System.Web 的引用。

回答by Alexei Levenkov

Despite your comment that you've added reference to System.Web to the project it is still most likely reason of what you are seeing.

尽管您评论说您已经在项目中添加了对 System.Web 的引用,但这仍然是您所看到的最有可能的原因。

The classes you see are coming from System.Dll, not System.Web.Dll.

您看到的类来自 System.Dll,而不是 System.Web.Dll。

回答by gcoleman0828

Just to add some input incase someone else gets hung up on this. If you are in a Unit test project or a Class Library that links back to an MVC project, you may have references to System.Web.Mvc. This DOES NOT INCLUDE System.Web. You must add both if you need something Mvc specific (like a SelectList) AND HttpContext

只是为了添加一些输入以防其他人对此感到困惑。如果您在链接回 MVC 项目的单元测试项目或类库中,您可能会引用System.Web.Mvc。这不包括System.Web。如果您需要特定于 Mvc 的东西(如 SelectList)和 HttpContext,则必须同时添加两者