ActionResult and MVCActionResult in MVC
ActionResult is the result type returned after the Controller method is executed. The Controller method can return a type that is directly or indirectly inherited from the ActionResult abstract class. If the returned type is not ActionResult, the Controller converts the result to a ContentResult type. The default ControllerActionInvoker calls the ActionResult. ExecuteResult method to generate a response result.
1. diagram of the ActionResult derived class
2. Several Common actionresults
1,ContentResult
Return simple plain text content. You can use the ContentType attribute to specify the type of the Response Document and the ContentEncoding attribute to specify the character encoding of the Response document. You can use the Content method in the Controller class to conveniently return the ContentResult object. If the Controller method returns a non-ActionResult object, MVC will generate a ContentResult object based on the ToString () content of the returned object.
C # code Replication
public ContentResult RSSFeed() { Story[] stories = GetAllStories(); // Fetch them from the database or wherever // Build the RSS feed document string encoding = Response.ContentEncoding.WebName; XDocument rss = new XDocument(new XDeclaration("1.0", encoding, "yes"), new XElement("rss", new XAttribute("version", "2.0"), new XElement("channel", new XElement("title", "Example RSS 2.0 feed"), from story in stories select new XElement("item", new XElement("title", story.Title), new XElement("description", story.Description), new XElement("link", story.Url) ) ) ) ); return Content(rss.ToString(), "application/rss+xml"); }
2,EmptyResult
Returns an empty result. If the Controller method returns a null value, MVC converts it to the EmptyResult object.
3,RedirectResult
Indicates a connection jump, which is equivalent to the Response. Redirect method in ASP. NET. The corresponding Controller method is Redirect.
C # code Replication
public override void ExecuteResult(ControllerContext context) { if (context == null) { throw new ArgumentNullException("context"); } if (context.IsChildAction) { throw new InvalidOperationException(MvcResources.RedirectAction_CannotRedirectInChildAction); } string destinationUrl = UrlHelper.GenerateContentUrl(Url, context.HttpContext); context.Controller.TempData.Keep(); context.HttpContext.Response.Redirect(destinationUrl, false /* endResponse */);}
4,RedirectToRouteResult
It also indicates a redirection. MVC will generate a Url address based on the specified route name or route information (RouteValueDictionary) and then call Response. Redirect to Redirect. The corresponding Controller methods are RedirectToAction and RedirectToRoute.
5,ViewResult:
A view result, which generates response Content Based on The View template. The corresponding Controller method is View.
6,PartialViewResult:
It indicates a part of the view result, which is essentially the same as ViewResult, but some views do not support the master. Corresponding to ASP. NET, ViewResult is equivalent to a Page, while PartialViewResult is equivalent to a UserControl. The corresponding Controller method is PartialView.
7,HttpUnauthorizedResult:
Indicates an unauthorized access error. MVC sends a 401 response status to the client. If form authentication mode = "Forms" is enabled in web. config, the 401 status redirects the Url to the specified loginUrl link.
8,JavaScriptResult:
It is essentially a text content, but only Response. contentType is set to application/x-javascript. This result should be consistent with that of MicrosoftMvcAjax. when js scripts are used together, the client determines the Response after receiving the Ajax Response. contentType value. If it is application/x-javascript, eval directly executes the returned response content. The Controller method corresponding to this result type is JavaScript.
9,JsonResult:
Represents a JSON result. MVC sets Response. ContentType to application/json, and serializes the specified object to a Json representation through the JavaScriptSerializer class. Note that by default, MVC does not allow GET requests to return JSON results. To remove this restriction, set the JsonRequestBehavior attribute to JsonRequestBehavior. AllowGet when the JsonResult object is generated. The Controller method corresponding to this result is Json.
C # code Replication
class CityData { public string city; public int temperature; } public JsonResult WeatherData() { var citiesArray = new[] { new CityData { city = "London", temperature = 68 }, new CityData { city = "Hong Kong", temperature = 84 } }; return Json(citiesArray); }
10,FilePathResult, FileContentResult, and FileStreamResult: These three classes inherit from FileResult to indicate the content of a file. The difference between the three classes is that FilePath transfers the file to the client through a path, and FileContent uses binary data, fileStream is transmitted through Stream. Controller provides a File overload method for these three File Result types.
FilePathResult: send a file directly to the client.
C # code Replication
public FilePathResult DownloadReport() { string filename = @"c:\\files\\somefile。pdf"; return File(filename, "application/pdf", "AnnualReport。pdf"); }
FileContentResult: byte bytes are returned to the client.
C # code Replication
public FileContentResult GetImage(int productId) { var product = productsRepository.Products.First(x => x.ProductID == productId); return File(product.ImageData, product.ImageMimeType); }
FileStreamResult: returned stream
C # code Replication
public FileStreamResult ProxyExampleDotCom() { WebClient wc = new WebClient(); Stream stream = wc.OpenRead(http://www.studyofnet.com/); return File(stream, "text/html"); }
What role does ActionResult play in mvc?
ActionResult is irrelevant to the MVC mode.
Actually used in earlier versions
Void ActionName (){
RenderView ("viewpage ");
}
This method is also possible.
ASP. net mvc adds this return value to increase the testability of the program.
Various results can be obtained when writing unit tests.
Can ActionResult in MVC call the content of ActionResult in other controls?
Cannot be called directly. You can only jump to the Action and return the result.