Page 1 of 1

How add dll reference to ewcontroller.cs (v2021)

Posted: Wed Mar 15, 2023 10:55 pm
by lfernandes

I'm using ASPNETMAKER 2021 to create a custom page using dropzone.js to upload multiple files to a server folder. However, I need to add the dll references in the ewcontroller.cs file to solve the problem.

It is important to note that I have already tried to add it to the Nugets option and it had no effect. I'm having to add it directly to the file and run build.bat to make the project run without errors.

We are using the following code:

public async Task<IActionResult> UploadFiles(IFormFile filedata)
{
     var files = HttpContext.Request.Form.Files;
     if (files.Any())
     {
         foreach (var file in files)
         {
             if (file.Length > 0)
             {
                 //Getting FileName
                 var fileName = Path.GetFileName(file.FileName);
                 //Assigning Unique Filename (Guid)
                 var myUniqueFileName = Convert.ToString(Guid.NewGuid().ToString("N"));
                 //Getting file Extension
                 var fileExtension = Path.GetExtension(fileName);
                 // concatenating FileName + FileExtension
                 var newFileName = String.Concat(myUniqueFileName, fileExtension);
                 await using var target = new MemoryStream();
                 await file.CopyToAsync(target);
                 var physicalPath = $"{new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "UploadedFiles")).Root}{$@"{fileName}"}";
                 string filePath = $"/UploadedFiles/{fileName}";
                 await using FileStream fs = System.IO.File.Create(physicalPath);
                 await file.CopyToAsync(fs);
                 fs.Flush();
             }
         }
         return Json(new { status = true, Message = "Files Uploaded Successfully!" });
     }
return View();
}

When compiling, the following error occurs:

ewcontroller.cs(407,40):error CS0103: The name "Path" does not exist in the current context
ewcontroller.cs(414,54):error CS0246: The type or namespace name "MemoryStream" cannot be found (missing a using directive or assembly reference?)
ewcontroller.cs(416,72):error CS0246: The type or namespace name "PhysicalFileProvider" cannot be found (are you missing an assembly reference?)
ewcontroller.cs(416,85):error CS0246: The type or namespace name "FileStream" cannot be found (missing a using directive or an assembly reference?)
Error: Failed to publish project.


Re: How add dll reference to ewcontroller.cs (v2021)

Posted: Thu Mar 16, 2023 8:57 am
by MichaelG

v2021 used .NET 5.0, if you add your own Nuget packages, make sure you specify a version which supports .NET 5.0.


Re: How add dll reference to ewcontroller.cs (v2021)

Posted: Tue Mar 21, 2023 11:02 pm
by lfernandes

I need to manually add the Microsoft.Extensions.FileProviders reference to the ewcontroller.cs file so that the code works without errors.
Note: In the NuGet Packages option, this library option is not available.


Re: How add dll reference to ewcontroller.cs (v2021)

Posted: Tue Mar 21, 2023 11:09 pm
by lfernandes

For my code to work I have to manually add the following references to the ewcontroller.cs file:
using System.IO;
using Microsoft.Extensions.FileProviders;

Remembering that NuGets Packages does not have the "Microsoft.Extensions.FileProviders" reference option available. How can I make ASPNETMAKER generate this file containing these references?


Re: How add dll reference to ewcontroller.cs (v2021)

Posted: Wed Mar 22, 2023 8:42 am
by MichaelG