41

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.

4
  • Is HttpContext.Current set to anything (does it "resolve"), or you only do not see HttpContext.Current.Request.Files.Keys? Is this code from your controller, or another class? If this is from a method in another class, then make sure you pass HttpContext.Current into it somehow. Either as a parameter to the method you're calling, or have a public member (of HttpContext type) that would accept this value before you call the method.
    – Floremin
    Feb 27, 2013 at 17:06
  • HttpContext.Current is not global to your application. It is set in actions and views that serve the request, but outside of the request cycle (things like models, utility classes, etc.) it doesn't exist. If you need it there, you must pass it in from an action or view where it does exist as @Floremin says. Feb 27, 2013 at 17:26
  • OK - Yes, this is within in an action in my controller. HttpContext resolves, but not Current. I find HttpContext.Request.Files without the static Current, and this seems to work fine. Not sure why static Current is not there, maybe not accessible from Controller "context"? Hopefully I am not "missing" anything not using the static. Thanks!
    – Nick
    Feb 28, 2013 at 15:47
  • The sample code is for WebForms, not MVC, and the only difference is which reference to use. HttpContext.Request.Files is fine. Mar 22, 2013 at 13:33

3 Answers 3

113

Try prefixing it with 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'.

@Chris' answer points and explains the underlying reason.

1
  • 4
    @Chris has answered it why you have to prefix HttpContext with System.Web, even if you have included System.Web in your using statements. because controller already has a HttpContext property and if you use without System.Web, it refers to its local property which does not have current.
    – Niraj
    Jun 17, 2015 at 5:50
65

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.

1
  • 1
    Even though this is a (very) old answer, I'd like to add that you can still access the HttpContext from HttpContextBase by doing HttpContext.ApplicationInstance.Context Apr 20, 2022 at 0:25
4

Very simple add library

using System.Web;

and replace

context.Response -> HttpContext.Current.Response

means

context -> HttpContext.Current

and your problem is solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.