Is it possible for a controller method to return a view if called restfully and if called, via JavaScript, would return a JsonResult. My motivation is that I want to have the freedom to implement my view however I want to do this WITHOUT having to create two controller methods (one for each separate scenario...see elaboration below).
If let's say I type in www.example.com/person/get?id=232
in the browser, I would want the Get(int id)
method to do something like the following:
public ActionResult Get(int id)
{
Person somePerson = _repository.GetPerson(id);
ViewData.Add("Person", somePerson);
return View("Get");
}
But if let's say this same controller method is called via jQuery:
//controller method called asynchronously via jQuery
function GetPerson(id){
$.getJSON(
"www.example.com/person/get", //url
{ id: 232 }, //parameters
function(data)
{
alert(data.FirstName);
} //function to call OnComplete
);
}
I would want it to act like the following:
public JsonResult Get(int id)
{
Person somePerson = _repository.GetPerson(id);
return Json(somePerson);
}