The ActionResult of MVC

Source: Internet
Author: User

One, all controllers inherit from System.Web.Mvc.Controller.

Currently, the ASP. NET MVC3 provides many implementations of ActionResult in the SYSTEM.WEB.MVC namespace.

Where ActionResult is an abstract class, all of the result is inherited from it, so if the return value of an action is ActionResult, you can return a value of any of the following types, but if you limit the return value to any of the following result, It is only possible to return data of the specified type.

    • Contentresult
    • Emptyresult
    • Fileresult
    • Httpstatuscoderesult
    • Httpnotfoundresult
    • Httpunauthorizedresult
    • Javascriptresult
    • Jsonresult
    • Redirectresult
    • Redirecttorouteresult
    • Viewresultbase
    • Partialviewresult
    • ViewResult
        PublicContentresult Index () {Return Content ("Test");//Browser Display test}PublicEmptyresult Index () {ReturnNew Emptyresult ();//Browser display blank}PublicFileresult Index () {Return File (Server.MapPath ("~/demo.jpg"),"Application/x-jpg","Demo.jpg");//Browser Direct Download Demo.jpg}PublicHttpnotfoundresult Index () {return Httpnotfound ();//Reported 404 errors}PublicHttpunauthorizedresult Index () {ReturnNew Httpunauthorizedresult ();//Unauthorized pages, jump to/account/logon}PublicJavascriptresult Hello () {String js ="Alert (' Are you okay? ‘);";Return JavaScript (JS);//The page shows alert (' Are you okay? ‘);} Do not execute this JS, to execute this JS can be in any view <script src= "@Url. Action (" Hello ")" type= "Text/javascript" ></script>}PublicJsonresult Index () {var jsonobj =New{Id =1, Name ="Xiao Ming", Sex ="Man", like ="Football"};Return Json (Jsonobj, jsonrequestbehavior.allowget);//Returns a JSON that can output this code to the JS processing display}PublicRedirectresult Index () {Return Redirect ("~/demo.jpg");//Can jump to any one of the pathsReturn Redirect ("Http://www.baidu.com");Return Redirect ("/list"); }PublicRedirecttorouteresult Index () {Return Redirecttoroute (//Jump to the specified actionNew{controller ="Home" getname< Span style= "color: #800000;" > "}); } public ViewResult Index () { return View (); // This is the most common, return the specified view //< Span style= "color: #008000;" >return View ("List"); //return View ("/user/list"); public Partialviewresult Index () {return Partialview (); // partial view, which can be introduced as a part in another view, roughly the same as view}     

ActionResult of MVC

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.