此代码在服务器中运行一次,然后我们再次单击提交按钮服务器显示“www.example.com 当前无法处理此请求。HTTP 错误 500”
这是我的控制器
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create([Bind("AboutId,Title,ImagePath,ShortContent,LongContent,RecordStatus,CreatedDate,Seokeywords")] AboutTbl aboutTbl, IFormFile FormFile)
{
if (ModelState.IsValid)
{
//----
string newFileName;
var fileName = ContentDispositionHeaderValue.Parse(FormFile.ContentDisposition).FileName.Trim('"');
int index = fileName.LastIndexOf('.');
string onlyName = fileName.Substring(0, index);
string fileExtension = fileName.Substring(index + 1);
var abtrepo = _aboutTblRepository.FindwithImagePath(fileName);
if (abtrepo != null)
{
newFileName = onlyName + DateTime.Now.ToString("yyyy-MM-ddHHmmtt") + "." + fileExtension;
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "images", "About", newFileName);
aboutTbl.ImagePath = newFileName;
using (System.IO.Stream stream = new FileStream(filePath, FileMode.Create))
{
FormFile.CopyTo(stream);
}
}
else
{
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "images", "About", FormFile.FileName);
aboutTbl.ImagePath = fileName;
using (System.IO.Stream stream = new FileStream(filePath, FileMode.Create))
{
FormFile.CopyTo(stream);
}
}
//----
byte recordStatus = (byte)Common.CommonEnums.RecordStatus.ACTIVE;
aboutTbl.RecordStatus = (byte?)recordStatus;
DateTime createdDate = DateTime.Now;
aboutTbl.CreatedDate = createdDate;
_aboutTblRepository.CreateAbout(aboutTbl);
_notyf.Success("About added successfully");
return RedirectToAction(nameof(Index));
}
return View(aboutTbl);
}
这是 AboutTblRepository
public void CreateAbout(AboutTbl aboutTbl)
{
_context.Add(aboutTbl);
_context.SaveChanges();
}
接口 IAboutTblRepository
public void CreateAbout (AboutTbl aboutTbl);