Search code examples
c#asp.netasp.net-mvcasp.net-mvc-4httpcontext

HttpContext.Current not Resolving in MVC 4 Project


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

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


Solution

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

    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.