【发布时间】: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