• ActionResult types in MVC2


    Raj Kaimal's Blog

    http://weblogs.asp.net/rajbk/archive/2010/05/03/actionresult-types-in-mvc2.aspx

     In ASP.NET MVC, incoming browser requests gets mapped to a controller action method. The action method returns a type of ActionResult in response to the browser request. A basic example is shown below:
    public class HomeController : Controller
    {
    public ActionResult Index()
    {
    return View();
    }
    }

    Here we have an action method called Index that returns an ActionResult. Inside the method we call the View() method on the base Controller. The View() method, as you will see shortly, is a method that returns a ViewResult.

    The ActionResult class is the base class for different controller results. The following diagram shows the types derived from the ActionResult type.



    ASP.NET has a description of these methods
    ContentResult – Represents a text result.
    EmptyResult – Represents no result.
    FileContentResult – Represents a downloadable file (with the binary content).
    FilePathResult – Represents a downloadable file (with a path).
    FileStreamResult – Represents a downloadable file (with a file stream).
    JavaScriptResult – Represents a JavaScript script.
    JsonResult – Represents a JavaScript Object Notation result that can be used in an AJAX application.
    PartialViewResult – Represents HTML and markup rendered by a partial view.
    RedirectResult – Represents a redirection to a new URL.
    RedirectToRouteResult – Represents a result that performs a redirection by using the specified route values dictionary.
    ViewResult – Represents HTML and markup rendered by a view.

    To return the types shown above, you call methods that are available in the Controller base class. Instead of creating instances of ActionResult or it’s inherited type, you call the helper methods on the Controller class and return it. A list of these methods are shown below.

    The helper methods on the controller base class and the ActionResult type they return are listed here (ref: http://msdn.microsoft.com/en-us/library/dd410269.aspx):

    Action Result
    Helper Method
    Description

    ViewResult
    View
    Renders a view as a Web page.

    PartialViewResult
    PartialView
    Renders a partial view, which defines a section of a view that can be rendered inside another view.

    RedirectResult
    Redirect
    Redirects to another action method by using its URL.

    RedirectToRouteResult
    RedirectToAction

    RedirectToRoute
    Redirects to another action method.

    ContentResult
    Content
    Returns a user-defined content type.

    JsonResult
    Json
    Returns a serialized JSON object.

    JavaScriptResult
    JavaScript
    Returns a script that can be executed on the client.

    FileResult
    File
    Returns binary output to write to the response.

    EmptyResult
    (None)
    Represents a return value that is used if the action method must return a null result (void).


    Methods without an ActionResult return type
    The MVC framework will translate action methods that do not return an ActionResult into one. Consider the HomeController below which has methods that do not return any ActionResult types. The methods defined return an int, object and void respectfully.
    public class HomeController : Controller
    {
    public int Add(int x, int y)
    {
    return x + y;
    }

    public Employee GetEmployee()
    {
    return new Employee();
    }

    public void DoNothing()
    {

    }
    }

    When a request comes in, the Controller class hands internally uses a ControllerActionInvoker class which inspects the action parameters and invokes the correct action method. The CreateActionResult method in the ControllerActionInvoker class is used to return an ActionResult. This method is shown below. If the result of the action method is null, an EmptyResult instance is returned. If the result is not of type ActionResult, the result is converted to a string and returned as a ContentResult.
    protected virtual ActionResult CreateActionResult(ControllerContext controllerContext, ActionDescriptor actionDescriptor, object actionReturnValue) {
    if (actionReturnValue == null) {
    return new EmptyResult();
    }

    ActionResult actionResult = (actionReturnValue as ActionResult) ??
    new ContentResult { Content = Convert.ToString(actionReturnValue, CultureInfo.InvariantCulture) };
    return actionResult;
    }



    In the HomeController class above,
    the DoNothing method will return an instance of the EmptyResult()
    Renders an empty webpage
    the GetEmployee() method will return a ContentResult which contains a string that represents the current object
    Renders the text “MyNameSpace.Controllers.Employee” without quotes.
    the Add method for a request of /home/add?x=3&y=5 returns a ContentResult
    Renders the text “8” without quotes.


    Unit Testing

    The nice thing about the ActionResult types is in unit testing the controller. We can, without starting a web server, create an instance of the Controller, call the methods and verify that the type returned is the expected ActionResult type. We can then inspect the returned type properties and confirm that it contains the expected values.

    Enjoy!

    Sulley: Hey, Mike, this might sound crazy but I don't think that kid's dangerous.
    Mike: Really? Well, in that case, let's keep it. I always wanted a pet that could kill me.

  • 相关阅读:
    20160402_[转]联合体(union)的使用方法及其本质
    20160402_C语言位操作符的使用
    20160402_多道程序环境中的设备抢占问题
    20160402_TCP/IP协议簇
    20160402_TCP连接的建立、终止和状态转换
    20160402_C++中的内存对齐
    20160127_Android程序完全退出的方法
    Redis持久化
    github慢!怎么办?
    mybatis-plus-generator: 自动生成entity,mapper,service,controller的代码
  • 原文地址:https://www.cnblogs.com/triggor/p/2086317.html
Copyright © 2020-2023  润新知