我目前正在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);
}
以下是一些屏幕截图。 Add Product 404/Access Denied
答案 0 :(得分:0)
我怀疑您在表格中包含该文件?因此该文件在模型中,因此它不会找到路线。在模型中添加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