在MVC 4项目中无法解析HttpContext.Current [英] HttpContext.Current not Resolving in MVC 4 Project

查看:283
本文介绍了在MVC 4项目中无法解析HttpContext.Current的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用ImageResizer(来自ImageResizing点网).我通过NuGet安装了用于MVC的ImageResizer.但是当我使用示例中的以下代码时:

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:

//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();
}

foreach中的"HttpContext.Current.Request.Files.Keys"无法解析吗?我的使用方法正确,Visual Studio没有提供解决"选项.

The "HttpContext.Current.Request.Files.Keys" in the foreach is not resolving? I have my usings correct and Visual Studio offers no "Resolve" options.

推荐答案

问题是Controller类具有名为HttpContext的公共属性(请参见http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.httpcontext.aspx).

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).

这意味着当您尝试在控制器中不加任何限制地使用它时,它将解析为本地属性,而不是System.Web.HttpContext.该属性的类型是HttpContextBase,它确实具有一个Request属性,该属性可以满足您的要求(尽管请注意,它与您从System.Web.HttpContext获得的类不是同一类.

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.

这篇关于在MVC 4项目中无法解析HttpContext.Current的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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