【问题标题】:Server.MapPath() not working outside controller ASP.NET MVCServer.MapPath() 在控制器 ASP.NET MVC 外部不起作用
【发布时间】:2022-06-16 16:50:06
【问题描述】:

我的服务功能:

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

我收到一条错误消息:The name 'Server' does not exist in the current context。 当我在控制器中使用 Server.MapPath 时不会出现此错误,但我使用的是 3 层架构,需要在控制器外部的功能上使用它。

【问题讨论】:

    标签: c# asp.net asp.net-mvc


    【解决方案1】:

    你应该使用HttpContext
    HttpContext.Current.Server.MapPath()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-18
      • 2014-04-04
      • 1970-01-01
      • 2014-01-03
      • 1970-01-01
      • 2016-01-30
      • 1970-01-01
      • 2017-03-31
      相关资源
      最近更新 更多