Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 839
Nick Avatar asked Feb 27 '13 16:02

Nick


People also ask

Why HttpContext current is null?

It won't work in the scheduling related class because relevant code is not executed on a valid thread, but a background thread, which has no HTTP context associated with. Overall, don't use Application["Setting"] to store global stuffs, as they are not global as you discovered.

How do I find HttpContext current?

If you're writing custom middleware for the ASP.NET Core pipeline, the current request's HttpContext is passed into your Invoke method automatically: public Task Invoke(HttpContext context) { // Do something with the current HTTP context... }

How use HttpContext current in .NET Core?

ASP.NET Core apps access HttpContext through the IHttpContextAccessor interface and its default implementation HttpContextAccessor. It's only necessary to use IHttpContextAccessor when you need access to the HttpContext inside a service.


1 Answers

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.

like image 66
user2343180 Avatar answered Sep 23 '22 15:09

user2343180