Asp.net mvc 自定义异常ASP.NET MVC
控制器类Asp.net mvc 自定义异常ASP.NET MVC,asp.net-mvc,Asp.net Mvc,控制器类 public class CustomExceptionHandlerFilter : FilterAttribute, IExceptionFilter { public void OnException(ExceptionContext filterContext) { filterContext.ExceptionHandled = true; filterContext.Result =
public class CustomExceptionHandlerFilter : FilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
filterContext.ExceptionHandled = true;
filterContext.Result = new ViewResult()
{
ViewName = "Exception"
};
}
}
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new CustomExceptionHandlerFilter());
}
protected void Application_Start()
{
FastReport.Utils.Config.WebMode = true;
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
RegisterDependencyResolution();
InitializeAutoMapper();
}
[HttpPost]
[CustomExceptionHandlerFilter]
公共行动结果新(PurchaseInvMasterModel模型)
{
//试一试
//{
int companyid=int.Parse(((UserIdentity)User.Identity.companyid);
使用(SalesMgmtEntities dbCon=new SalesMgmtEntities())
{
model.PaymentDate=DateTime.ParseExact(model.DueDate,“dd-MM-yyyy”,CultureInfo.InvariantCulture).ToString();
model.Date=DateTime.ParseExact(model.Date,“dd-MM-yyyy”,CultureInfo.InvariantCulture.ToString();
var inv=AutoMapper.Mapper.Map(模型);
inv.CustomerId=model.CustomerId;
var customer=\u customerService.getcustomerbyd(model.CustomerId);
inv.DueDate=DateTime.ParseExact(model.DueDate,“dd-MM-yyyy”,CultureInfo.InvariantCulture);
存货代码=GetCode();
inv.AmountInWords=model.AmountInWords;
inv.TotalAmount=model.TotalAmount;
inv.NetAmount=model.TotalAmount;
inv.InvoiceAmount=model.InvoiceAmount;
库存日期=转换到日期时间(型号日期);
inv.IsActive=true;
inv.CreatedBy=((UserIdentity)User.Identity).Name;
inv.CreatedDate=DateTime.Now;
inv.ModifiedBy=((UserIdentity)User.Identity).Name;
inv.ModifiedDate=DateTime.Now;
inv.CompanyId=CompanyId;
inv.InvoiceType=model.InvoiceType;
库存状态=“S”;
inv.CustomerNotes=model.CustomerNotes;
inv.TermsAndConditions=model.TermsAndConditions;
inv.transport运费=model.transport运费;
dbCon.PurchaseInvMasters.Add(inv);
dbCon.SaveChanges();
字符串businesCode=((UserIdentity)User.Identity).BusinessCode;
JavaScriptSerializer js=新的JavaScriptSerializer();
IEnumerable detail=js.Deserialize(model.DetailString);
foreach(详细的var项目)
{
PurchaseInvDetail d=新的PurchaseInvDetail();
d、 主Id=库存Id;
d、 IsActive=true;
d、 Date=DateTime.Now;
d、 描述=项目。描述;
d、 TaxId=项目.TaxId;
d、 数量=项目数量;
d、 费率=项目费率;
d、 InventoryId=item.InventoryId;
d、 折扣=项目。折扣;
d、 折扣类型=项目。折扣类型;
d、 金额=项目金额;
var quantity=\u stockService.GetStockById(item.InventoryId).quantity-item.Qty;
_stockService.UpdateStock(新库存{ProductID=item.InventoryId,Quantity=Quantity});
如果(业务代码==“02”)
{
d、 BatchNo=项目。BatchNo;
d、 ExpiryDate=DateTime.ParseExact(item.Expiry,“dd-MM-yyyy”,CultureInfo.InvariantCulture);
d、 奖金=物品。奖金;
}
db.PurchaseInvDetails.Add(d);
}
db.SaveChanges();
返回JsonResponse(“成功保存记录”,
状态:AjaxResponseStatus.成功,
重定向Url:Url.Action(“详细信息”、“销售发票”)
);
}
//}
//捕获(例外情况除外)
//{
//返回JsonResponse(例如消息,状态:AjaxResponseStatus.Error);
//}
}
配置文件
[HttpPost]
[CustomExceptionHandlerFilter]
public ActionResult New(PurchaseInvMasterModel model)
{
//try
//{
int companyid = int.Parse(((UserIdentity)User.Identity).CompanyId);
using (SalesMgmtEntities dbCon = new SalesMgmtEntities())
{
model.PaymentDate = DateTime.ParseExact(model.DueDate, "dd-MM-yyyy", CultureInfo.InvariantCulture).ToString(); ;
model.Date = DateTime.ParseExact(model.Date, "dd-MM-yyyy", CultureInfo.InvariantCulture).ToString();
var inv = AutoMapper.Mapper.Map<PurchaseInvMasterModel, PurchaseInvMaster>(model);
inv.CustomerId = model.CustomerId;
var customer = _customerService.GetCustomerById(model.CustomerId);
inv.DueDate = DateTime.ParseExact(model.DueDate, "dd-MM-yyyy", CultureInfo.InvariantCulture);
inv.Code = GetCode();
inv.AmountInWords = model.AmountInWords;
inv.TotalAmount = model.TotalAmount;
inv.NetAmount = model.TotalAmount;
inv.InvoiceAmount = model.InvoiceAmount;
inv.Date = Convert.ToDateTime(model.Date);
inv.IsActive = true;
inv.CreatedBy = ((UserIdentity)User.Identity).Name;
inv.CreatedDate = DateTime.Now;
inv.ModifiedBy = ((UserIdentity)User.Identity).Name;
inv.ModifiedDate = DateTime.Now;
inv.CompanyId = companyid;
inv.InvoiceType = model.InvoiceType;
inv.Status = "S";
inv.CustomerNotes = model.CustomerNotes;
inv.TermsAndConditions = model.TermsAndConditions;
inv.TransportFreight = model.TransportFreight;
dbCon.PurchaseInvMasters.Add(inv);
dbCon.SaveChanges();
string businesCode = ((UserIdentity)User.Identity).BusinessCode;
JavaScriptSerializer js = new JavaScriptSerializer();
IEnumerable<PurchaseInvDetailModel> detail = js.Deserialize<IEnumerable<PurchaseInvDetailModel>>(model.DetailString);
foreach (var item in detail)
{
PurchaseInvDetail d = new PurchaseInvDetail();
d.MasterId = inv.Id;
d.IsActive = true;
d.Date = DateTime.Now;
d.Description = item.Description;
d.TaxId = item.TaxId;
d.Qty = item.Qty;
d.Rate = item.Rate;
d.InventoryId = item.InventoryId;
d.Discount = item.Discount;
d.DiscountType = item.DiscountType;
d.Amount = item.Amount;
var quantity = _stockService.GetStockById(item.InventoryId).Quantity - item.Qty;
_stockService.UpdateStock(new Stock { ProductID = item.InventoryId, Quantity = quantity });
if (businesCode == "02")
{
d.BatchNo = item.BatchNo;
d.ExpiryDate = DateTime.ParseExact(item.Expiry, "dd-MM-yyyy", CultureInfo.InvariantCulture);
d.Bonus = item.Bonus;
}
db.PurchaseInvDetails.Add(d);
}
db.SaveChanges();
return JsonResponse("Record saved succussfully.",
status: AjaxResponseStatus.Success,
redirectUrl: Url.Action("Detail", "SaleInvoice")
);
}
//}
//catch (Exception ex)
//{
// return JsonResponse(ex.Message, status: AjaxResponseStatus.Error);
//}
}
看法
@{
ViewBag.Title=“错误”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
例外情况
发生错误
处理您的请求时发生未知错误。
我在asp.net mvc中实现了自定义异常,但我不工作。。不显示包含异常详细信息的视图。视图名称“异常”…是否正在点击过滤器?视图是否在共享文件夹中?是,视图是否在共享文件夹中?是否点击过滤器?视图是否在共享文件夹中?是共享文件夹中的视图
<customErrors mode="On" />
@{
ViewBag.Title = "Error";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Exception</h2>
<hgroup>
<h1>Error Occured </h1>
<h1 class="alert alert-danger">An unknown error occurred while processing your request.</h1>
</hgroup>