{"id":435,"date":"2014-10-15T12:07:07","date_gmt":"2014-10-15T12:07:07","guid":{"rendered":"http:\/\/mairwa.com\/wordpress\/?p=435"},"modified":"2014-10-15T12:07:07","modified_gmt":"2014-10-15T12:07:07","slug":"apicontroller-and-controller-differences","status":"publish","type":"post","link":"http:\/\/mairwa.com\/wordpress\/?p=435","title":{"rendered":"Apicontroller and controller differences"},"content":{"rendered":"<p>ASP.NET MVC<\/p>\n<p>public class TweetsController : Controller {<\/p>\n<p>\/\/ GET: \/Tweets\/<\/p>\n<p>[HttpGet]<\/p>\n<p>public ActionResult Index() {<\/p>\n<p>return Json(Twitter.GetTweets(), JsonRequestBehavior.AllowGet);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>ASP.NET Web API<\/p>\n<p>public class TweetsController : ApiController {<\/p>\n<p>\/\/ GET: \/Api\/Tweets\/<\/p>\n<p>public List&lt;Tweet&gt; Get() {<\/p>\n<p>return Twitter.GetTweets();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>The first major difference you will notice is that actions on Web API controllers do not return views, they return serialized\u00a0data.<br \/>\nThe MVC controller shows URL examples matching the default route pattern of \u201c{controller}\/{action}\/{id}\u201d.<br \/>\nThe Web API controller shows URL examples matching it\u2019s default route pattern of \u201capi\/{controller}\/{id}\u201d.<br \/>\nIn Web API controller no need to convert data to json format.<br \/>\nUse Controller to render your normal views. API controller action only return data that is serialized and sent to the client.<br \/>\nwhen ever you are using ajax call use Web API controller.<\/p>\n","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\">ASP.NET MVC public class TweetsController : Controller { \/\/ GET: \/Tweets\/ [HttpGet] public ActionResult Index() { return Json(Twitter.GetTweets(), JsonRequestBehavior.AllowGet); } } ASP.NET Web API public class TweetsController : ApiController { \/\/ GET: \/Api\/Tweets\/ public List&lt;Tweet&gt; Get() { return Twitter.GetTweets(); } } The first major difference you will notice is that actions on Web API controllers do not return views, they&hellip; <a href=\"http:\/\/mairwa.com\/wordpress\/?p=435\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,3,10],"tags":[],"class_list":["post-435","post","type-post","status-publish","format-standard","hentry","category-net-framework","category-c-vb","category-mvc","xfolkentry"],"_links":{"self":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/435","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=435"}],"version-history":[{"count":0,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/435\/revisions"}],"wp:attachment":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=435"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}