Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • MVC Action Results

    • 0
    • 1
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 454
    Comment on it

    MVC Action Results:-

    ActionResult is a return type of a controller method,it's also called an action method. In MVC by default the controller actions will return the ActionResult Object. You can return various types of results as ActionResults. Which we will decide, How the output wants the render on browsers.

    MVC provides various types of action results. In MVC application you open System. Web. MvC using Reflector, you will see there are several derived result types that inherits from the abstract class ActionResult. They are below:---

    1. ActionResult: -- ActionResult is an abstract class. By default MVC controller action will return the ActionResult object. It can use to exploit polymorphism and dynamism.When you are returning different types of view dynamically than the ActionResult is the best thing.

    public ActionResult Template()
    {
       if (IsHtmlView)
         return View(); // returns simple ViewResult
       else
         return Json(); // returns JsonResult view
    } 
    

    2. View Result: - ViewResult is derived from the ActionResult class. You can use view results when we want to render a specified view to the response stream.

    public ViewResult About()
    {
      return View(); //returns simple ViewResult(view with empty model and holds only html) 
    } 
    

    3. JsonResult: - JsonResult class serializes a data object to json format. It also returns back data in json format.

    public JsonResult getEmplyeeData()
    {
        Employee obj = new Employee();
        obj.EmployeeCode = "500";
        obj.EmployeeName = "Rahul";
        return Json(obj,JsonRequestBehavior.AllowGet);
    }
    

    3. FileStreamResult: - It returns a file to the client (browser), which is provided by a Stream. Leak Sends binary content to the response by using a stream instance.

     public ActionResult SetImage(string imageName)
            {
                try
                {
                    byte[] bytes = RenderImage(imageName);
                    FileContentResult ff = new FileContentResult(bytes, "application/octet-stream");   //It Returns a file to the client
                    return ff;
                }
                catch (Exception)
                {
    
                }
    
                return null;
            }
    
       public byte[] RenderImage(string imagePath)
            {
                var model = "C:\Users\Public\Pictures\Sample Pictures\profile.jpg"
                if (model.Content.GetType() == typeof(System.Net.Http.ByteArrayContent))
                {
                 return ((System.Net.Http.ByteArrayContent)(model.Content)).ReadAsByteArrayAsync().Result;
                }
                return null;
    
            }
    

    4. PartialViewResult :- It also derived from the ActionResult class. In MVC all results derived from the ActionResult class. PartialViewResult use to render a specified partial view to the response stream.

    5.EmptyResult: - An empty response is returned.

    6. RedirectResult :- It Performs an HTTP redirection to a specified url.

    Public RedirectResult GoToAbout ()
    {
      Return RedirectPermanent ("AboutUs"); 
    } 
    

    7. RedirectToRouteResult: - Performs an HTTP redirection to a URL that is determined by the routing engine, based on giving route data.

    8. JavaScriptResult :- Using JavaScriptResult you returns a piece of JavaScript code that can be executed on the client browser.

     public JavaScriptResult GetElementData()
     {
        string jsData = @"var x=document.getElementById('divUserData');jsData.innerHTML= '****Java Script Result****';";
                return JavaScript(jsData);
      }
    

    9. ContentResult: - ContentResult represents a user-defined content type. You can write content to the response stream without a view.

       public ContentResult UserInfo()
       {
          return Content("Rahul Joshi","text/plain",Encoding.UTF8);
        }
    

    10. FilePathResult: - It Returns a file to the client.

    public FilePathResult Download(string file, Guid GuID)
    { 
        return File(FileStream, "application/octet-stream", fileName);
    }
    

 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: