-1

My service function:

public void Create(PostAddRequest model)
        {
            var featuredImageFile = "/Content/img/uploads/" + Guid.NewGuid() + "_" + model.FeaturedImage.FileName;
            var post = new Post()
            {
                Title = model.Title,
                CategoryId = model.CategoryId,
                Content = model.Content,
                Description = model.Description,
                PersonId = HttpContext.Current.User.Identity.GetUserId(),
                FeaturedImagePath = featuredImageFile,
                CreatedOn = DateTime.UtcNow
            };
            model.FeaturedImage.SaveAs(Server.MapPath(featuredImageFile));
            _unitOfWork.PostRepository.AddPost(post);
            _unitOfWork.SaveChanges();
        }

I'm getting an error saying: The name 'Server' does not exist in the current context. This error doesn't occur when I use Server.MapPath in a controller, but I'm using a 3 layer architecture and need to use it on a function outside a controller.

ELOL
  • 183
  • 1
  • 1
  • 9

1 Answers1

0

You should use HttpContext
HttpContext.Current.Server.MapPath()
How to use in class library:
Right click on References -> Add reference -> Select Assemblies section in the left side -> Search for System.Web -> Select it -> Click Ok -> Done

  • .....I've been trying find a solution for the past two hours, only to realize my `Content` folder is in `Presentation Layer` and my services are in "BLL". Any idea how to target the other project? – ELOL Jun 16 '22 at 08:54
  • Right click on References -> Add reference -> Select Assemblies section in left side -> Search for `System.Web' -> Select it -> Click Ok -> Done. – Payam Shariati Jun 16 '22 at 09:01