C# HttpContext.Current 未在 MVC 4 项目中解析
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15117379/
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
HttpContext.Current not Resolving in MVC 4 Project
提问by Nick
I am wanting to use ImageResizer (from ImageResizing dot net). I installed ImageResizer for MVC via NuGet. But when I go to use the following code from the example:
我想使用 ImageResizer(来自 ImageResizing dot net)。我通过 NuGet 为 MVC 安装了 ImageResizer。但是当我使用示例中的以下代码时:
//Loop through each uploaded file
foreach (string fileKey in HttpContext.Current.Request.Files.Keys)
{
HttpPostedFile file = HttpContext.Current.Request.Files[fileKey];
if (file.ContentLength <= 0) continue; //Skip unused file controls.
//The resizing settings can specify any of 30 commands.. See http://imageresizing.net for details.
//Destination paths can have variables like <guid> and <ext>, or
//even a santizied version of the original filename, like <filename:A-Za-z0-9>
ImageResizer.ImageJob i = new ImageResizer.ImageJob(file, "~/uploads/<guid>.<ext>", new ImageResizer.ResizeSettings(
"width=2000;height=2000;format=jpg;mode=max"));
i.CreateParentDirectory = true; //Auto-create the uploads directory.
i.Build();
}
The "HttpContext.Current.Request.Files.Keys" in the foreach is not resolving? I have my usings correct and Visual Studio offers no "Resolve" options.
foreach 中的“HttpContext.Current.Request.Files.Keys”没有解析?我的使用正确,Visual Studio 没有提供“解决”选项。
采纳答案by Chris
The problem is that the Controller
class has a public property called HttpContext
(see http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.httpcontext.aspx).
问题是Controller
该类有一个名为HttpContext
(参见http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.httpcontext.aspx)的公共属性。
This means that when you attempt to use it without any qualification in the controller it resolves to the local property and not System.Web.HttpContext
. The type of the property is HttpContextBase
which does have a Request
property that would do what you want (though note that it isn't the same class as you would get from System.Web.HttpContext
.
这意味着当您尝试在控制器中没有任何限定的情况下使用它时,它会解析为本地属性而不是System.Web.HttpContext
. 属性的类型HttpContextBase
确实具有Request
可以执行您想要的操作的属性(但请注意,它与您从System.Web.HttpContext
.
回答by user2343180
Try prefixing it with System.Web.
尝试使用前缀 System.Web.
If I try System.Web.HttpContext.Current
, then Current is there, but if I try HttpContext.Current
, then it doesn't recognize 'Current'. I do have System.Web
in my using statements, but I still seem to be required to specify it in order to get access to 'Current'.
如果我尝试System.Web.HttpContext.Current
,则 Current 存在,但如果我尝试HttpContext.Current
,则它无法识别“Current”。System.Web
我的 using 语句中确实有,但我似乎仍然需要指定它才能访问“当前”。
回答by Dilip0165
Very simple add library
非常简单的添加库
using System.Web;
and replace
并替换
context.Response -> HttpContext.Current.Response
means
方法
context -> HttpContext.Current
and your problem is solved.
你的问题就解决了。