How to capture the exception generated by a controller?

1

I have the following code that generates a report in PDF:

public void reporte(int ID)
{

            try{

            System.Data.DataSet ds = new System.Data.DataSet("Hoja_Salida");
            DataTable dtEmpresa = Empresa.Obtener(Convert.ToInt32(SessionManager.Item(SessionItems.fkEmpresa)));
            DataTable dtHojaSalida = DocumentoDetalle.ObtenerReporteHojaSalida(ID);



            dtEmpresa.TableName = "Encabezado";
            dtHojaSalida.TableName = "Detalle";

            ds.Tables.Add(dtEmpresa.Copy());
            ds.Tables.Add(dtHojaSalida.Copy());

            ds.Tables["Encabezado"].Columns.Remove("logo");
            ds.Tables["Encabezado"].Columns.Add("logo",Type.GetType("System.Byte[]"));



            Reportes.Reportepdf rp = new Reportes.Reportepdf();
            rp.SetDataSource(ds);

            BinaryReader stream = new BinaryReader(rp.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat));
            Response.ClearContent();
            Response.ClearHeaders();
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment; filename=" + "Reporte.pdf");
            Response.AddHeader("content-length", stream.BaseStream.Length.ToString());
            Response.BinaryWrite(stream.ReadBytes(Convert.ToInt32(stream.BaseStream.Length)));
            Response.Flush();
            Response.Close();
            }

            catch(Exception ex)
            {
                 throw new Exception(ex.Message);
            }
}

and I execute it from the view in the following way:

 @{if(item.Folio != null){ <li><a href="@Url.Action("reporte", new { IDD= @item.ID})" class="fa fa-print">Hoja de salida sin costos</a></li> } else{}}

Someone can give me an idea of how to catch the exception and send it to view, to be able to send it to another controller through ajax.

    
asked by mvega 27.01.2017 в 20:08
source

1 answer

0

One way to solve the problem you are presenting is to add the content of the exception to a session variable with Session["NombreDeLaVariableDeSesion"] :

public void reporte(int ID)
{
    try
    {
        //Código de tu reporte
    }
    catch(Exception ex)
    {
         HttpContext.Session["ExcepcionEnReporte"] = ex.Message;
    }
}

Then, in the view where the report was invoked, if an exception was generated we redirected to Controller :

@{
    if(item.Folio != null)
    { 
        <li><a href="@Url.Action("reporte", new { IDD= @item.ID})" class="fa fa-print">Hoja de salida sin costos</a></li>

        if(!string.IsNullOrEmpty(Session["ExcepcionEnReporte"].ToString())){
            RedirectToAction("ShowException", "Exception");
        }
    } 
    else { }
}

Now, in the ExceptionController , we read the session variable, we save it in the database and then we address it to the View to show the error message:

public class ExceptionController : Controller
{
    public string ExceptionMessage = string.empty;

    public ActionResult ShowException()
    {
        ExceptionMessage = HttpContext.Session["ExcepcionEnReporte"] != null ? HttpContext.Session["ExcepcionEnReporte"].ToString() : string.Empty;

        if(!string.IsNullOrEmpty(ExceptionMessage))
            //Guardado de la excepción en la base de datos

        //Guardamos el contenido de la excepción para luego mostrarla en la vista
        ViewBag.SpanExceptionMessage = ExceptionMessage;

        //Regresas un View para que se muestre el mensaje del error
        return View();
    }
}

Finally, we only show the message in the view using a <span> :

<span class="help-block">@ViewBag.SpanExceptionMessage</span>
    
answered by 27.01.2017 / 23:57
source