Skip to content
This repository has been archived by the owner on Jan 20, 2021. It is now read-only.

clementi/Mvc.Jsonp

Repository files navigation

Mvc.Jsonp

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

End of life

This repository has been archived and is now read-only.

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.

About

A simple ActionResult and Controller superclass for adding JSONP support to MVC 3.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published