What are all the ASP.Net MVC Action Results?
Asked Answered
U

2

14

Is there a list of all the ASP.Net MVC action results and their uses?

I've been busily using ActionResult for almost everything but I know that's not correct and that I should be using more specific action results.

I've Googled this but cannot find a list. We've just bought the Wrox book but it's more than a week away from delivery and I'd like to read up on this well before then.

Can you also roll your own and is that documented somewhere?

Ukulele answered 29/9, 2009 at 4:19 Comment(0)
Q
24

If you open System.Web.Mvc using Reflector, you will see that there are several derived types that inherit from the abstract class ActionResult. They are:

System.Web.Mvc.ContentResult
System.Web.Mvc.EmptyResult
System.Web.Mvc.FileResult
    System.Web.Mvc.FileContentResult
    System.Web.Mvc.FilePathResult
    System.Web.Mvc.FileStreamResult
System.Web.Mvc.HttpUnauthorizedResult
System.Web.Mvc.JavaScriptResult
System.Web.Mvc.JsonResult
System.Web.Mvc.RedirectResult
System.Web.Mvc.RedirectToRouteResult
System.Web.Mvc.ViewResultBase
    System.Web.Mvc.PartialViewResult
    System.Web.Mvc.ViewResult

Yes, you can roll your own by inheriting from the abstract class ActionResult. You can study one or more of the ActionResults in the list above with Reflector to get a feel for how this would be done.

The source code is also available here:

http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24471

Quiroz answered 29/9, 2009 at 4:26 Comment(2)
In the case of ASP.NET MVC, the source is available. It may be more convenient to use reflector, but there is extra value (comments etc) in the source.Invariant
Thanks Marc. Shall take a look into that as well. In the mean time I managed to create my own ActionResult that returned the word "Slappy". So that was fun. :) Thanks to all.Ukulele
V
0

If you're looking this up for ASP.NET MVC 5, add HttpStatusCodeResult to the list.

Also it's worth to mention that the helper methods that return the corresponding action result, do not have the word "Result" at the end. For example to return a HttpNotFoundResult, you will write return HttpNotFound("status description");.

ActionResult Class page on MSDN has the full list of result classes and the helper methods.

Variation answered 16/5, 2017 at 17:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.