Skip to content

Instantly share code, notes, and snippets.

@dkarzon
Created June 11, 2013 23:20
Show Gist options
  • Save dkarzon/5761657 to your computer and use it in GitHub Desktop.
Save dkarzon/5761657 to your computer and use it in GitHub Desktop.
Helper method for automatically returning a View or Json
protected ActionResult ViewOrJson(object model)
{
return ViewOrJson(null, model);
}
protected ActionResult ViewOrJson(string viewName, object model)
{
var useJson = false;
//check if its an API call
if (Request.Headers["Something"] != null)
{
useJson = true;
}
if (Request.IsAjaxRequest())
{
useJson = true;
}
if (useJson)
{
return Json(model);
}
else
{
if (string.IsNullOrEmpty(viewName))
{
return View(model);
}
else
{
return View(viewName, model);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment