MVC 5 Showing Errors

ASP.NET MVC 5 offers an easy way to display error.

Step 1: Setup the error View

You create an error view “Error.cshtml”

   1: @model System.Web.Mvc.HandleErrorInfo

   2: @{

   3:     ViewBag.Title = "Error";

   4: }

   5: <h1 class="text-danger">Error.</h1>

   6: <h2 class="text-danger">An error occurred while processing your request.</h2>

   7: <h2 class="text-danger">Details: </h2>

   8: <h3 >@Model.Exception.Message</h3>

   9: @Html.ActionLink("Back", @Model.ActionName, @Model.ControllerName)

 

Step 2: Return the Error View in your Controller Action

   1: try

   2: {

   3:  ...

   4: }

   5:  

   6: catch(Exception ex)

   7: {

   8:     return View("Error",new HandleErrorInfo(ex,"MyController","MyAction"));

   9: }

 

Reference:

http://www.dotnetcurry.com/aspnet-mvc/1068/aspnet-mvc-exception-handling

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s