Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • ASP.NET MVC : ActionResult Types

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 441
    Comment on it

    An ActionResult is a return type of a controller method in MVC. We can return various types of results as ActionResult.

    Here, we will discuss about some of the ActionResults available as part of ASP.NET MVC.

    1) ViewResult

    It renders a specified view to the response stream.

    public ViewResult ViewResult()
    {
        return View("ViewResult");
    }

    2) ContentResult

    We use Content Type when we need to return any text from a Controller action.

    public ActionResult Index()
    {
        return Content("Hello");
    }
    

    3) RedirectToActionResult

    We can redirect to another Action based on the input values using the RedirectToAction type.

    public ActionResult Index()
    {
    
        return RedirectToAction("Catalogue", "Product");
    }
    

    4) RedirectToRouteResult

    When we need to redirect to a route defined in Global.asax, we will use the RedirectToRoute object.As part of our sample application, we have a custom route defined with the name sample. This will route to the Index action inside the Sample Controller.

    public ActionResult Index()
    {
        return RedirectToRoute("sample");
    }
    

    5) FileResult

    File is used to return the content of a file to the browser.

    public ActionResult Index()
    {
        return File("Web.config", "text/html");
    }
    

    6) JSONResult

    Here we are sending the result as a file to the client using JSON notation.

    public ActionResult Index()
    {
        return Json("JSON","text/html", JsonRequestBehavior.AllowGet);
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: