【问题标题】:how to get Exception message when trying to display create/Edit view尝试显示创建/编辑视图时如何获取异常消息
【发布时间】:2019-04-28 20:48:25
【问题描述】:

我有两条业务规则,我尝试申请:

  1. 用户只有在所有发票都关闭时才能插入新发票。
  2. 发票关闭后,用户无法编辑她。

发票有两种状态:当前、已关闭、已付款和已取消

为此,我在业务层的创建方法中实现了我的业务逻辑

当用户尝试点击提交按钮时,httppost 操作方法调用业务代码。

但我想在 httpget 创建/编辑操作方法中应用这些规则,这样,当用户尝试单击添加按钮以显示创建/编辑视图时,他会分别获得创建和编辑的异常消息

这是我的代码

   //business logic
   public AddInvoice(Invoice invoice)
   {
       var invoicesCount = Context.Invoices.Count(x=>x.State != 
       InvoiceState.Closed);//InvoiceState is enum
       if (invoicesCount > 0)
            throw new BusinessReulesException("you should close all your 
          invoices before insert");

          Context.Invoices.Add(invoice);
          Context.SaveChanges();
   }

   public UpdateInvoice(Invoice invoice)
   {
       if (Context.Entry(invoice).State == EntityState.Detached)
  Context.Invoices.Attach(invoice);

       if (invoices.State == InvoiceState.Closed)
           throw new BusinessReulesException("you can't update an closed invoices );

       Context.Entry(invoice).State =EntityState.Modified;
       Context.SaveChanges();
   }

  //controller code
  [httpGet]
  public ActionResult Create()
  {
     //how to apply business logic for catch Exception here
  }
  [httpPost]
  public ActionResult Create(Invoice invoice)
  {
      if(ModelState.isValide){
      try{
           invoiceBll.AddInvoice(invoice);
           return RedirectToAction("Index");
      }
      catch(BusinessRulesException ex){
             ViewBag.Message = ex.Message;
      }
   }
}
//the same thing for update
//the rest of code

有什么想法吗?

【问题讨论】:

    标签: c# asp.net-mvc entity-framework


    【解决方案1】:

    您必须编写一个单独的视图(例如Shared/ErrorPage.cshtml)来显示您存储在 ViewBag 中的错误消息。我想这就是它的用途,对吧?

    <h2>You encountered an error!</h2>
    <div class="flashing-red" id="ErrorMessage">@ViewBag.Message</div>
    <img src="SadKitten.png">
    

    然后在你的action方法中,你想return that specific view而不是默认值:

    [httpPost]
    public ActionResult Create(Invoice invoice)
    {
        if(ModelState.isValide){
        try{
             invoiceBll.AddInvoice(invoice);
             return RedirectToAction("Index");
        }
        catch(BusinessRulesException ex){
             ViewBag.Message = ex.Message;
             return View("~/Shared/ErrorPage.cshtml");  //<<<<-- this is new
        }
     }
    

    注意:呈现代码抛出的原始异常消息供最终用户阅读may not be the best practice

    【讨论】:

      【解决方案2】:

      为了保持代码干净,您可以使用HandleError 内置过滤器。喜欢:

      [HandleError(View = "Your_Error_View")]
      public ActionResult Create(Invoice invoice)
      {
        if(ModelState.isValide)
        {
          try
          {
             invoiceBll.AddInvoice(invoice);
             return RedirectToAction("Index");
          }
          catch(BusinessRulesException ex)
          {
               ViewBag.Message = ex.Message;
          }
        }
      }
      

      ASP.Net MVC HandleError 属性提供了一个内置的异常过滤器。 ASP.NET MVC 中的 HandleError 属性可以应用于操作方法以及控制器或全局级别,用于处理控制器和操作级别的异常。在创建我们的应用程序时,HandleError 属性会自动包含在 Global.asax.cs 中,并在 FilterConfig.cs 中注册,如下所示。

      更多处理asp.net mvc错误的方法:

      Web.Config 自定义错误

      Controller.OnException 方法

      HttpApplication Application_Error 事件

      使用 Retrace 通过 .NET 分析收集异常

      自定义中间件

      编辑

      它可以在方法级别指定,您可以为每个方法使用不同的错误视图,或者您可以为整个控制器指定一个错误视图。

      可以将错误数据传递到错误视图,以便您可以在那里显示它

      【讨论】:

      • 我想在 get action 方法中处理异常,而不是在 post 中,以防止在抛出异常时显示创建/编辑方法
      • 当然,你可以在每个方法中处理它,GET,POST,PUT...你可以为它们指定不同的视图,或者你可以为整个控制器使用一个视图,它是给你。您可以将数据传递到错误视图并在那里显示,您可以使用部分视图。我喜欢我的代码是干净的,所以我更喜欢这样,而且你可以重用大部分代码,检查c-sharpcorner.com/article/…
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多