loader
Loading...
WelCome TO My Blog
Asp.Net MVC 08 January 2018

Improving Application Responses with Custom Action Results In Asp.Net MVC

By dhiren

The purpose of the Controller

The purpose of the controller within the MVC pattern is to respond to user input. In ASP.NET MVC, the action method is the granular unit of response to user input. The action method is ultimately responsible for handling a user request and outputting the response that is displayed to the user, which is typically HTML.

All action method inherits from ActionResult Class. Which has one method ExecuteResult ().

 

public abstract class ActionResult

{

 public abstract void ExecuteResult(ControllerContext context);

}

 

There are different type of build in action result provide in MVC.

ActionResult

Helper Method

Description

ViewResult

View

ViewResult Renders a view as a web page.

PartialViewResult

PartialView

As the name describe PartialViewResult renders the partial view.

RedirectResult

Redirect

When you want to redirect to another action method we will use RedirectResult

RedirectToRouteResult

RedirectToRoute

Redirect to another action method

ContentResult

Content

Returns a user-defined content type

JsonResult

Json

When you want to return a serialized JSON object

JavaScriptResult

JavaScript

Returns a script that can be executed on the client

FileResult

File

Returns a binary output to write to the response

EmptyResult

(None)

returns a null result

 

Create Custom Method to download CSVFile:-

 

Step 1:  Create Model Class:

 

public class Employee

    {

        public string Name { get; set; }

    }

 

Step 2: Create method in HomeController.

    public ActionResult GetEmployeeDetails()

        {

            var list = new Employee();

            var a = new Employee() { Name = "Dhiren" };

            var b = new Employee() { Name = "Vatsal" };

            var c = new Employee() { Name = "Mihir" };

            var d = new Employee() { Name = "Riddhesh" };

            var EmployeeList = new List<Employee>();

            EmployeeList.Add(a);

            EmployeeList.Add(b);

            EmployeeList.Add(c);

            EmployeeList.Add(d);

 

            return new CSVResult(EmployeeList, "Employee");//This is custom action result method.

        }

 

Step 3:- Create custom action result class in Infrastructure Folder.

  public class CSVResult : FileResult

    {

        private IEnumerable _data;

 

        public CSVResult(IEnumerable data, string fileName) : base("text/csv")

        {

            _data = data;

            FileDownloadName = fileName;

        }

 

        protected override void WriteFile(HttpResponseBase response)

        {

            var builder = new StringBuilder();

            var stringWriter = new StringWriter(builder);

 

            foreach (var item in _data)

            {

                var properties = item.GetType().GetProperties();

                foreach (var prop in properties)

                {

                    stringWriter.Write(GetValue(item, prop.Name));

                    stringWriter.Write(", ");

                }

                stringWriter.WriteLine();

            }

 

            response.Write(builder);

        }

 

        public static string GetValue(object item, string propName)

        {

            return item.GetType().GetProperty(propName).GetValue(item, null).ToString() ?? "";

        }

    }

 

Step 4: Run the project click on download button.

 

Click Here To Download

 

Hope you like it. Stay tuned for more..foot-in-mouth

Leave a Comment

Only registered users can comment.


View Comments