CoinCotroller

Searching...
Saturday, June 23, 2012

Create custom ActionResult in MVC 3

In my previous post i show the use of different type of actionresult. that's all are built in MVC framework.

you can inherit any class from ActionResult class. it contain one method ExecuteResult that is responsible for generate the output.In this post we create RssActionResult class that generate Rss Xml data using XDocument Api to generate the xml data and write it to response.


    Public MustInherit Class RssActionResult
    Inherits ActionResult

     End Class
     

above is just abstract class that inherits from ActionResult Base class. We can use this class for generate Rss feed from different type of data collection.

Public Class RssActionResult(Of T)
    Inherits RssActionResult

    Public Property DataItems As IEnumerable(Of T)
    Public Property Title As String
    Public Property Formatter As Func(Of T, XElement)

    Sub New(ByVal title As String, ByVal dataitems As IEnumerable(Of T), ByVal formatter As Func(Of T, XElement))

        Me.Title = title
        Me.Formatter = formatter
        Me.DataItems = dataitems

    End Sub

    Public Overrides Sub ExecuteResult(context As System.Web.Mvc.ControllerContext)
        Dim response As HttpResponseBase = context.HttpContext.Response

        response.ContentType = "application/rss+xml"

        Dim rss As String = GenerateXML(response.ContentEncoding.WebName)

        response.Write(rss)

    End Sub

    Function GenerateXML(ByVal encoding As String) As String
        Dim rss = New XDocument(New XDeclaration("1.0", encoding, "yes"),
                               New XElement("rss", New XAttribute("version", "2.0"),
                               New XElement("channel",
                                           New XElement("title", Title),
                                                 DataItems.Select(Formatter))))

        Return rss.ToString

    End Function
End Class
    

above is a Generic class that is derived from our RssActionResult abstract class. In second class we create one helper method that is for generating XML from XDocument api.

for data storage we create three Public property so its easy to unit test our custom actionresult. In ExecuteResult method we simply get the response object from current controller context and set its response contentType to Application/rss+xml and generating XML string from our helper method using default encoding that support by browser and write XML result to response.

   Function RSS() As RssActionResult
   Dim person = {New Person With {.FirstName = "Pravin", .LastName = "Bhudiya", .Gender = "Male"},
                New Person With {.FirstName = "Prabhat", .LastName = "Chad", .Gender = "Male"},
                New Person With {.FirstName = "Anil", .LastName = "verma", .Gender = "Male"}}


 Return New RssActionResult(Of Person)("PersonList", person, Function(p)
            
             Return New XElement("person", New XAttribute("FirstName", p.FirstName),
                                           New XAttribute("LastName", p.LastName),
                                           New XAttribute("Gender", p.Gender))
                                             
                                             End Function)

        End Function

Above is simple use of our RSSActionResult.we create list of person class with three simple property, passing it to constructor ,and in third parameter we pass function that format each person info to XML element.


0 comments:

Post a Comment

Write your review about this post.