Different result types

(ASP.NET MVC)


Hi,

In asp.net mvc, we can return response to the user from controller action. To return response from controller action, the asp.net mvc uses ActionResult abstract class. The definition of abstract
class is

public abstract class ActionResult
{
   protected ActionResult();
  
    public abstract void ExecuteResult(ControllerContext context);
}


The executeresult method process the result of an action method. We can return different types of responses to the to the user with the help of different types of result classes those are derived from ActionResult base class.

The different types of result classes are:

ContentResult ->  returns/writes some content to the response stream.

FileResult ->  returns/writes file content to the response stream/user.

HttpNotFoundResult -> returns/writes httpnotfoundresult error message to the user.

ViewResult -> writes specified view to the response stream.

JavaScriptResult -> returns/writes specified javascript code to the user.

JsonResult -> writes specified json object to the response stream.

RedirectResult -> It send the redirect url to the client with the status code http 301/302.

Example:

    public ActionResult GetView()
    {
         //return new ViewResult();
        return View();
    }
   
We can return response to the user in two ways. The first way is creating the instance of  specified result type as shown in the above example, the commented statement. The second way is using the action helper methods those methods are defined in the controller class. It will take care of creating the instance of specified result types and render the result to the response stream. It's better to use the helper methods.


   public ActionResult Gettext()
        {
            try
            {
            //for xml "application/xml"
               // ContentResult

            return Content("Hello World!","text/plain");
            }
            catch(Exception)
            {}
            return null;
        }
       
It renders  plain text to the response.

public ActionResult GetFileData()
        {
            try
            {
            byte[] bytes = System.IO.File.ReadAllBytes(Server.MapPath(@"~/Images/orderedList0.png"));

               // FileResult
            return File(bytes, "image/png");
            }
            catch(Exception)
            {}
            return null;
        }
       
The above method renders the specified file content to the response.

 public ActionResult HttpError()
        {
        try
        {
            //HttpNotFoundResult
            return HttpNotFound("Custom error message. file not found");
        }
        catch(Exception)
        {}

        return null;
        }

The above method renders the specified custom error message to the response.

    public ActionResult JsView()
        {
            //ViewResult
            return View();
        }

 public ActionResult GetJson()
        {
            try
            {
                List lst = new List();
                lst.Add(new Employee { Name="Jim",Age=20 });
                lst.Add(new Employee { Name = "Tom", Age = 30 });

                //JsonResult
                return Json(lst,JsonRequestBehavior.AllowGet);
            }
            catch(Exception)
            { }

            return null;
        }

        //HTTP 302 STATUS CODE
        public ActionResult RedirectExample()
        {
            try
            {
               // RedirectResult
                return Redirect("http://msn.in");
            }
            catch(Exception)
            { }

            return null;
        }


       The below action method  returns http status code "HTTP 301" to the client,i.e.
       it is redirected permananently and resources are not available on the specified url.
      
        public ActionResult RedirectPermanentExample()
        {
           try
            {
             
                return RedirectPermanent("http://msn.in");
            }
            catch(Exception)
            { }

            return null;
        }

        //HTTP 302
        public ActionResult RedirectToActionTest()
        {
            try
            {
              
                return RedirectToAction("GetFileData");
            }
            catch(Exception)
            { }

            return null;
        }


      
       //HTTP 301
        public ActionResult RedirectToActionPmntTest()
        {
           try
            {
                return  RedirectToActionPermanent("GetFileData");
            }
            catch(Exception)
            { }

            return null;
        }

        public ActionResult RedirectToRouteTest()
        {
            try
            {
                return RedirectToRoute(new RouteValueDictionary {{"controller","Home"},{ "action","Gettext"}});
            }
            catch(Exception)
            { }

            return null;
        }


        public ActionResult RedirectToRoutePermntTest()
        {
            try
            {
                return RedirectToRoutePermanent(new RouteValueDictionary { { "controller", "Home" }, { "action", "Gettext" } });
            }
            catch(Exception)
            { }

            return null;
        }

        public ActionResult ViewTest()
        {
           try
            {
               // return new ViewResult();
                return View();
            }
            catch(Exception)
            { }

            return null;
        }

        public ActionResult PartialViewTest()
        {
            try
            {
                return PartialView();
            }
           catch(Exception)
            { }

            return null;
        }
       
       
          public ActionResult EmptyTest()
        {
            try
            {
                return new EmptyResult();
            }
            catch(Exception)
            { }

            return null;
        }

        public ActionResult HttpUnauthTest()
        {
            try
            {
                return new HttpUnauthorizedResult();
            }
            catch(Exception)
            { }

            return null;
        }

        //It returns specified httpstatus code to the user.
        public ActionResult HttStatusCodeTest()
        {
            try
            {
                //bad request
                return new HttpStatusCodeResult(400);
            }
            catch(Exception)
            { }

            return null;
        }


Ratings


Average Rating: 0.00 by 0 users
Giri Prasad
4/13/2014
4/13/2014
Download PDF
Download Project

Comments






gp

nice one