Exception handling in asp.net MVC application

Sourav.Kayal
Posted by in ASP.NET MVC category on for Beginner level | Points: 250 | Views : 3335 red flag

How to handle exception in asp.net MVC application

Exception handling in asp.net MVC application


In this article we will learn how to use exception handling code in asp.net MVC application. Exception handling in MVC web application is very similar to normal web or .NET application. Same try-catch block can be implemented to handle exception in MVC application.

To understand the concept we will create small math application, where we will perform division operation on two numbers.

Create simple calculator class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace MVCApplication.Models
{
    public class Calculator
    {
        public Int32 FirstNumber { get; set;}
        public Int32 SecondNumber { get; set; }
    }
}
 

This calculator class is for perform calculation operation. It has only two properties called FirstNumber and SecondNumber and both are integer type.

 Create Calculate controller

Now, We have to create one controller class to handle http request. In our Calculation controller we have defined two actions. Index action is to invoke view(which is simple form) and ShowResult action

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 
namespace MVCApplication.Controllers
{
    public class CalculationController : Controller
    {
        public ActionResult Index()
        {
            return View("Index");
        }
        public ActionResult ShowResult()
        {
            Int32 A = Convert.ToInt32(Request.Form["FirstValue"]);
            Int32 B = Convert.ToInt32(Request.Form["SecondValue"]);
 
            try
            {
return Content((A / B).ToString());
            }
            catch (Exception ex)
            {
return Content("Error in Operation");
            }
        }
 
    }
}
 

will get call when form value will submit on button’s click event. Now, if we look inside of showResult() action, we will find that we are performing division operation within try block and within catch we are returning string “Error in Operation”. If we want we can return any view in place of String with in catch statement.

Create view

Here we will create a simple view with two textboxes and one button. Have a look on below code.

@model MVCApplication.Models.Calculator
 
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <title>Index</title>
</head>
<body>
    <div>
       <form action="Calculation/ShowResult" method="post">
        <input id="FirstValue" name="FirstValue" type="text" /><br />
        <input id="SecondValue" name="SecondValue" type="text" /><br />
        <input id="Submit1" type="submit" value="Submit" />
     </form>
    </div>
</body>
</html>

 

We have created one strong type view and in action attribute we have given path of showResult() action. It tells that when user will click on Submit button, the ShowResult action() will get called.

Here is sample output.
We will supply 0 as second number.


Here is output string


Conclusion:-

Here we have learned how to use simple try-catch block in MVC application. It is very similar with any .NET application. We can implement all exception handling features in MVC application like any other .NET application.

 

 

Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)