public ActionResult Delete(int id) { var post = NoticeService.GetById(id); if (post is null) { return(ResultData(null, false, "公告已经被删除!")); } var srcs = post.Content.MatchImgSrcs(); foreach (var path in srcs) { if (path.StartsWith("/")) { try { System.IO.File.Delete(HostingEnvironment.WebRootPath + path); } catch { } } } bool b = NoticeService.DeleteByIdSaved(id); return(ResultData(null, b, b ? "删除成功" : "删除失败")); }
public ActionResult Delete(int id) { var notice = NoticeService.GetById(id) ?? throw new NotFoundException("公告已经被删除!"); var srcs = notice.Content.MatchImgSrcs().Where(s => s.StartsWith("/")); foreach (var path in srcs) { try { System.IO.File.Delete(HostEnvironment.WebRootPath + path); } catch { } } bool b = NoticeService.DeleteByIdSaved(id); return(ResultData(null, b, b ? "删除成功" : "删除失败")); }