【问题标题】:ASP.Net Core Post Access DeniedASP.Net Core Post 访问被拒绝
【发布时间】:2018-09-24 07:40:25
【问题描述】:

我目前正在使用 ASP.Net Core 制作电子商务网站。我遇到了一个小难题。我去添加一个产品,填写表格,然后当我尝试提交表格时,它会打开我的 404/access 页面。

这是在 Ubuntu 16.04 Web 服务器上的生产模式。本地测试,效果不错。

代码:

[HttpGet]
public IActionResult AddProduct()
{
    return View(new ProductAddModel());
}

[HttpPost]
public async Task<IActionResult> AddProduct(ProductAddModel model, IFormFile file)
{
    if (ModelState.IsValid)
    {
        var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/images/Products", file.FileName);
        using (var stream = new FileStream(path, FileMode.Create))
        {
            await file.CopyToAsync(stream);
        }

        var user = await userManager.FindByNameAsync(User.Identity.Name);
        ProductDataModel dataModel = new ProductDataModel()
        {
            Name = model.Name,
            ShortDescription = model.ShortDescription,
            Description = model.Description,
            Category = model.Category,
            Game = model.Game,
            Price = model.Price,
            ImagePath = file.FileName,
            DeveloperUserId = user.Id
        };
        context.Products.Add(dataModel);
        context.SaveChanges();
        return RedirectToAction("Products", "Admin");
    }
    return View(model);
}

这里有一些屏幕截图。 Add Product 404/Access Denied

【问题讨论】:

  • 问题可能与Products 目录的写权限有关。如需确认,您可以将file.CopyToAsync 放入try catch block,然后检查产​​品是否成功添加到数据库中
  • 请显示 POST 操作的路线
  • 你说,是404!这意味着“未找到”并且不是访问错误。
  • @PoulBak,好吧,我将 404 页面与拒绝访问页面结合在一起。我了解 404 表示未找到...
  • @RomanMarusyk,路由是默认的。 “[控制器]/[动作]”。

标签: c# asp.net-core


【解决方案1】:

我怀疑您在表单中包含该文件?所以文件在模型中,所以它不会找到路径。在模型中添加 IFormFile:

[HttpPost]
public async Task<IActionResult> AddProduct(ProductAddModel model)
{
    if (ModelState.IsValid)
    {
        var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/images/Products", model.File.FileName);
        using (var stream = new FileStream(path, FileMode.Create))
        {
           await model.File.CopyToAsync(stream);
        }

        var user = await userManager.FindByNameAsync(User.Identity.Name);
        ProductDataModel dataModel = new ProductDataModel()
        {
            Name = model.Name,
            ShortDescription = model.ShortDescription,
            Description = model.Description,
            Category = model.Category,
            Game = model.Game,
            Price = model.Price,
            ImagePath = model.File.FileName,
            DeveloperUserId = user.Id
        };
        context.Products.Add(dataModel);
        context.SaveChanges();
        return RedirectToAction("Products", "Admin");
    }
    return View(model);
}

如果这不起作用,请显示 Startup.cs 和 AddProduct.cshtml

【讨论】:

  • 你好,如果你看上面,我包括 IFormFile。
  • @AveryShaw 您将 IFormFile 作为单独的参数包括在内。将其添加到模型中。
  • 我读错了回复。无论如何,我发现了这个问题。 “System.IO.IOException:权限被拒绝。”
猜你喜欢
  • 2020-08-09
  • 2017-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-15
  • 1970-01-01
  • 2018-08-21
  • 1970-01-01
相关资源
最近更新 更多