I find myself needing to return various JSON results to the client from my controller actions. I have created static "helper" methods of the type ContentResult. I want to be able to reuse these helpers in multiple projects, so I've created them in their own class, and someday soon I'll move them to their own assembly.
Here is my question: Some of these return helpers need access to the controller context, so I've create then as extension methods on Controller. I like the way Html Helper methods are extension methods on the HtmlHelper class, but I can't find a suitable controller property similar the the Html property on the View class, so I just extended controller. Below is the code from on return helper. I want to know if there is a better way to do this.
public static ContentResult AJAXRedirect(this Controller cont, string action,
string controller, object routeValues)
{
string url = cont.Url.Action(action, controller, routeValues);
string json = "{{\"redirect\": \"{0}\" }}";
json = string.Format(json, url);
return new ContentResult
{
Content = json,
ContentType = "text/x-json",
ContentEncoding = Encoding.UTF8
};
}