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