ASP.NET MVC’s ActionResult in depth

by Milad 22. February 2010 20:47

ActionResult Class is a valuable abstract class in ASP.NET MVC Framework. ActionMethods mostly return an ActionResult.

First let’s see what is an ActionMethod ?

Any class that inherits from System.Web.Mvc.Controller can use action methods to
serve web requests. The requirements for a method to be web-callable as an action method are well documented on http://www.asp.net/mvc. The method
■ Must be public
■ Cannot be a static method
■ Cannot be an extension method
■ Cannot be a constructor, getter, or setter
■ Cannot have open generic types
■ Is not a method of the Controller base class
■ Is not a method of the ControllerBase base class
■ Cannot contain ref or out parameters *

There are some other considerations about ActionMethods like they should not perform data access etc. But this post is about ActionResult class.

According to Reflector, ActionResult’s base type is System.Object and there are 14 classes that are Inherited from this class.

1. ContentResult  —Represents a text result
2. EmptyResult  —Represents no result
3. FileResult  —Represents a downloadable file (abstract class)
4. FileContentResult  —Represents a downloadable file (with the binary content)
5. FilePathResult  —Represents a downloadable file (with a path)
6. FileStreamResult  —Represents a downloadable file (with a file stream)
7. HttpUnauthorizedResult  —Represents the result of an unauthorized  HTTP
request
8. JavaScriptResult  —Represents a JavaScript script
9. JsonResult  —Represents a JavaScript Object Notation (JSON) result that can
be used in an AJAX application
10. RedirectResult  —Represents a redirection to a new URL
11. RedirectToRouteResult  —Represents a result that performs a redirection
given a route values dictionary
12. PartialViewResult  —Base class used to send a partial view to the response
13. ViewResult  —Represents HTML and markup
14. ViewResultBase  —Base class used to supply the model to the view and then render the view to the response

The important point of ASP.NET MVC Framework is it’s Extendibility. So the question here is how can we create a new ActionResult? The answer is quite simple!

namespace MyActionResults
{
    public class SimpleActionResult : System.Web.Mvc.ActionResult
    {
        private string msg;
        public SimpleActionResult(string Message)
        {
            msg = Message;
        }

        public override void ExecuteResult(System.Web.Mvc.ControllerContext context)
        {
            context.HttpContext.Response.Write(msg);
        }
    }
}

This ActionResult, prints a simple message to it’s HTTPContext(Like ContentResult). You can do so many interesting things with this class. Like in MvcContrib Library, There is an XMLResult Class, that puts an XML Output into the response stream.

P.S: This post is mostly inspired by Chapter 4 of Manning ASP.NET MVC in Action Book, titled The controller in depth. I highly recommend this book. :-)

Tags: , , , ,

A simple HTMLHelper for displaying Gravatars

by Milad 2. November 2009 20:44

Practically, The whole world knows about Gravatar(Globally Recognized Avatar).
I wanted to use this awesome service in one of my ASP.NET MVC applications, So I went to it’s documentation to see what should I do. [Well there is a great HTMLHelper in order to perform this, but I have a point!]

The big thing in constructing a Gravatar URL is how to get a string’s hexadecimal MD5 Hash. [as you may or may not know, you can do lots of stuff with MD5 Hash of anything!]
So … This is the answer to this question:

Function GetHash(ByVal text As String) As String
        Dim myhash As MD5CryptoServiceProvider = New MD5CryptoServiceProvider()

        Dim hashed = _
            myhash.ComputeHash(System.Text.UTF8Encoding.UTF8.GetBytes(text))

        Dim sb As StringBuilder = New StringBuilder()

        For Each b In hashed
            sb.Append(b.ToString("x2"))
        Next

        Return sb.ToString()
End Function

Tags: , , ,

ASP.net | Computer | Internet | Programming

Solving BlogEngine.NET Feed Error

by MiladKDZ 9. October 2009 12:21

My BlogEngine.NET 1.4.5.0’s Feed Page, encountered a weird error (Again tnx to MMR!). So I “Googled Around” to find it’s solution and unfortunately I didn’t find any. I tried to understand the really weird error and tracked/traced it into the program’s source code.
At last I solved it by changing the Time Difference setting in settings file to 0.

So … The time of my posts are not real but instead I have a pretty good Feed for this blog ;-)

BTW … Have you checked me out here?

Tags: , ,

BlogEngine.NET | Computer | Programming

Deeply Understand C# and .NET Framework

by MiladKDZ 29. September 2009 08:19

I think it is important for developers to know more about deep and low level aspects of their favorite Framework/Library.

For .NET developers this thing is extremely important as they can easily mess up design and code of their programs. Plus, the more you know, the less you face unknown problems ;-)

As a matter of fact, I started reading these two valuable books about Advanced C# and .NET Framework programming.

WroxProCs2008 ApressProCs2008

PS: Thanks to MMR for recommending me the first one.

Tags: ,

Programming | Computer

A few tips about ASP.net MVC

by MiladKDZ 2. May 2009 11:49

I started my first project with ASP.net MVC 2 days ago and I find it so much Interesting.

But I faced some Problems in it and I immediately found solutions using Google! (Thanks to MVC Community folks)

I also discussed writing a Helper method for HTML File Input Control in MVC with Dear Mahdi .

The most noticeable thing with MVC is the clean HTML that you get in runtime! Web development is so much fun with ASP.NET MVC ;-)

Tags: , ,

ASP.net | Computer | Programming

Enumerables

by MiladKDZ 6. March 2009 11:56

Just a little note!

The ExecuteQuery() Method from DataContext class, returns an instance of IEnumerable.
If you wanna determine if your Enumerable Instance is empty or not(Maybe you have performed a search operation using LIKE operator), you can use this piece of code:

YouEnumerableName.Equals(Enumerable.Empty<YourType>())

Tags: , ,

ASP.net | Computer | Programming

ASP.net File Upload Control

by MiladKDZ 24. February 2009 00:22

Well I'm working on a boring project but I'm learning too much! For example:

ASP.net's File Upload control has a 4MB  file length limit by default. [Basically every HTTPRequests should be less than 4MBs in an ASP.net Application by default]
So ... To overcome this limit, you can add this tag to your web.config file:

<httpRuntime maxRequestLength="4096"/>

Where you can replcace "4096" with any length in KBs.

µ

Tags: , ,

ASP.net | Computer | Programming