EmptyResult Return Type In ASP.NET MVC 5

Background

 
MVC controller returns many types of output to the view according to the data we need for the application. In this article we will learn about EmptyResult return type of MVC. So instead of going into the depth on the subject, let us start with its practical implementation.
 
To know more about the Action result types please refer my previous article,

What is ActionResult?


It is one of the type of output format in ASP.NET MVC which is shown to the client. 

What is EmptyResult?


The EmptyResult is a class in MVC which does not return anything at client site, its just like Void method .

EmptyResult is used when you want to execute logic return inside the controller action method but does not want any result back to the view then EmptyResult return type is very important .

 Key points
  • It does not return any output to the browser.
  • It shows the empty result set to the browser without adding the view.
  • Does not require to add the view.

Methods of EmptyResult

 
The following are the methods of EmptyResult class:
  • Equals: This method is used to check whether the two objects are equal or not.
  • ExecuteResult: This method is used to execute the specific result context.
  • Finalize: This method is used to free the memory which is occupied by object and allow to allocate another object in freed memory .
  • GetHashCode: It is used to get the numeric value which is used to identify and insert an object in hash based collection.
  • GetType: This method is used to check the what is the type of object .
  • MemberwiseClone: This method is used to create the shallow copy of the current object .
  • ToString: This method is used to convert the current result to the string .
I hope you have understand about the EmptyResult type from preceding brief summary, now let's implement it practically.

Step 1: Create an MVC application
  1. "Start", then "All Programs" and select "Microsoft Visual Studio 2015".
  2. "File", then "New" and click "Project" then select "ASP.NET Web Application Template", then provide the Project a name as you wish and click on OK.
  3. Choose MVC empty application option and click on OK
Step 3: Add controller class,

Add user and admin controller controller. Right click on Controller folder in the created MVC application and add the controller class as.


HomeController.cs
  1. public class HomeController : Controller  
  2. {  
  3.     // GET: for main view  
  4.     public EmptyResult EmptyData()  
  5.     {  
  6.         return new EmptyResult();  
  7.     }  
  8.           

In the above controller class we have added action method that is EmptyData which returns EmptyResult means nothing.

Lets run the application and see the output.
 
 
 
From above examples we have learned about the EmptyResult return type and its use.

Note
  • Apply proper validation before using it in your project.
Summary

I hope this article is useful for all the readers. If you have any suggestion please contact me.


Similar Articles