A simple ActionResult and Controller superclass for adding JSONP support to MVC 3.
C# Shell ASP
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
Mvc.Jsonp.Examples
Mvc.Jsonp.Tests
Mvc.Jsonp
packages
.gitignore
.semver
Mvc.Jsonp.nuspec
Mvc.Jsonp.sln
Mvc.Jsonp.snk
README.mdown
build.cmd
license.mdown
pack.cmd

README.mdown

Mvc.Jsonp

Mvc.Jsonp is a simple ActionResult and Controller base class for adding JSONP support to MVC 3.

Example

Just subclass your controller with JsonpControllerBase, like this:

public class HomeController : JsonpControllerBase
{
    public JsonpResult Index(string callback = "processStarships")
    {
        var starships = new List<Starship>
        {
            new Starship { Name = "Enterprise", Registry = "NCC-1701" },
            new Starship { Name = "Galaxy", Registry = "NCC-70637" },
            new Starship { Name = "Tikopai", Registry = "NCC-1800" }
        };

        return this.Jsonp(starships, callback, JsonRequestBehavior.AllowGet);
    }
}

If you don't want to make your controller a subclass of JsonpControllerBase, you can just return a JsonpResult, like this:

public class HomeController
{
    public JsonpResult Index(string callback = "processStarships")
    {
        var starships = new List<Starship>
        {
            new Starship { Name = "Enterprise", Registry = "NCC-1701" },
            new Starship { Name = "Galaxy", Registry = "NCC-70637" },
            new Starship { Name = "Tikopai", Registry = "NCC-1800" }
        };

        return new JsonpResult
        {
            Callback = callback,
            Data = starships,
            JsonRequestBehavior = JsonRequestBehavior.AllowGet
        };
    }
}

This will give you output of type application/javascript like this:

processStarships([{"Name":"Enterprise","Registry":"NCC-1701"},{"Name":"Galaxy","Registry":"NCC-70637},{"Name":"Tikopai","Registry":"NCC-1800"}]);

Building

To build with .NET 4.0, just run the included build.cmd. This will place Mvc.Jsonp.dll and its symbols in the output directory.

NuGet Package

To install Mvc.Jsonp from the NuGet repository, use this command:

PM> Install-Package Mvc.Jsonp

Acknowledgements

Mvc.Jsonp is based on http://stackoverflow.com/questions/758879/asp-net-mvc-returning-jsonp.