Created
June 21, 2011 09:32
-
-
Save jakejscott/1037528 to your computer and use it in GitHub Desktop.
Override asp.net mvc JsonResult with the fast ServiceStack JsonSerializer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class ApplicationController : Controller | |
{ | |
protected ActionResult RespondTo(Action<FormatCollection> format) { | |
return new FormatResult(format); | |
} | |
protected override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior) { | |
return new ServiceStackJsonResult { | |
Data = data, | |
ContentType = contentType, | |
ContentEncoding = contentEncoding | |
}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ServiceStackJsonResult : JsonResult | |
{ | |
public override void ExecuteResult(ControllerContext context) { | |
HttpResponseBase response = context.HttpContext.Response; | |
response.ContentType = !String.IsNullOrEmpty(ContentType) ? ContentType : "application/json"; | |
if (ContentEncoding != null) { | |
response.ContentEncoding = ContentEncoding; | |
} | |
if (Data != null) { | |
response.Write(JsonSerializer.SerializeToString(Data)); | |
} | |
} | |
} |
Hi,
Didn't find any reference to Action or FormatResult class,Can you add it in order to have a complete working sample ?
Thanks
Hi yhersh,
Have a look at
https://github.com/superlogical/Template/blob/master/src/Template.Website/Controllers/ApplicationController.cs
I am using RestfulRouting in that sample which makes it easy to setup Rails 3 like routing which is nicer that the asp.net mvc way of doing routes IMHO.
http://stevehodgkiss.github.com/restful-routing/
I've explained it a little bit better here:
https://github.com/superlogical/Template
Cheers
Jake
Looks great,
Thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oh and do away with the annoying JsonRequestBehavior B$