MVC implements separation of concerns and this one of the main advantages of using MVC over any other technology such as WebForms.For implementing separation of concerns MVC uses different derived classes and interfaces.For example action methods returns ActionResult.The ActionResult is a base class which has several derived classes.These classes are used for returning different types of responses from action methods.
Some of the important derived classes are:
ActionResult Name | Controller Method | Use |
ViewResult | View | used for rendering a view |
PartialViewResult | PartialView | used for rendering a partial view |
ContentResult | Content | for returning user-defined content type such as string. |
HttpNotFoundResult | HttpNotFound | Indicates the requested resource was not found. |
JsonResult | Json | used for returning Json object |
RedirectResult | Redirect | for redirecting to another action method using its URL |
JavaScriptResult | JavaScript | for sending JavaScript content to the response. |
HttpStatusCodeResult | for returning HTTP status code and description. | |
HttpNotFoundResult | HttpNotFound | indicates that the response was not found. |
So you can use the same action method to return different ActionReturn objects.
JSON
//JSON Result example class Employee { public string Name { get; set; } public string Id { get; set; } } public ActionResult Index() { //return json object var emp=new Employee{Id="1",Name="Emp1"}; return Json(emp); }
Redirection
//RedirectionResult example public ActionResult Index() { return Redirect(@"\seg1\seg2"); }
Content
//Content result example public ActionResult Index() { return Content("test reult"); }
Leave a Reply