繁体   English   中英

ASP.Net Core帖子访问被拒绝

[英]ASP.Net Core Post Access Denied

我目前正在ASP.Net Core中建立一个电子商务网站。 我遇到了一些困境。 我去添加产品,填写表格,然后当我尝试提交表格时,它会显示我的404 /访问页面。

这在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);
}

这是一些截图。 添加产品 404 /访问被拒绝

我怀疑您在表格中包含文件? 因此该文件在模型中,因此它不会找到路线。 在模型中添加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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM