ApiController and MVC Controllerhttp://forums.asp.net/t/1796046.aspx/1?ApiController+and+MVC+ControllerTue, 24 Apr 2012 04:26:36 -040017960464947070http://forums.asp.net/p/1796046/4947070.aspx/1?ApiController+and+MVC+ControllerApiController and MVC Controller <p>I have more of a general question. &nbsp;</p> <p>I am investigating the use of ApiController to possibly replace our MVC Controllers implementation. Since our MVC Controllers are used to render the view (via the various Actions), I am not if the ApiController will be a good replacement since it is mainly meant to create REST services and not for web applications.</p> <p>Do I have the general idea right here or am I completely wrong?</p> <p>Looking for some input since documentation is kind of scarce out there.</p> <p>Thanks</p> 2012-04-23T18:27:05-04:004947088http://forums.asp.net/p/1796046/4947088.aspx/1?Re+ApiController+and+MVC+ControllerRe: ApiController and MVC Controller <p>No, the two use different base classes that serve different purposes. &nbsp;You can see the difference just by looking at the types, <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.aspx"> MVC Controller</a> and <a href="http://msdn.microsoft.com/en-us/library/system.web.http.apicontroller(v=vs.108).aspx"> ApiController</a>.</p> <p>Here's a great quote from <a href="http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api"> this page</a>:</p> <p style="padding-left:30px"><strong>Note</strong><span>&nbsp;&nbsp;If you have worked with ASP.NET MVC, then you are already familiar with controllers. They work similarly in Web API, but controllers in Web API derive from the&nbsp;</span><strong>ApiController</strong><span>&nbsp;class instead of&nbsp;</span><strong>Controller</strong><span>&nbsp;class. The first major difference you will notice is that actions on Web API controllers do not return views, they return data.</span></p> <p style="padding-left:30px"></p> 2012-04-23T18:45:15-04:004947099http://forums.asp.net/p/1796046/4947099.aspx/1?Re+ApiController+and+MVC+ControllerRe: ApiController and MVC Controller <p>Thanks for your reply. &nbsp;</p> <p>I do understand that they are different types altogether but I am wondering about the general usage of each. &nbsp;We already know that MVC Controller are mainly used to receive HTTP Requests, do some logic and return a view or redirect to another page. &nbsp;That is all clear to me. &nbsp;What is not clear is the intended usage of the Web API Controller. &nbsp;Is its purpose mainly to create REST services? &nbsp;Can a razr view post requests to an API Controller (not through ajax)?</p> <p>Thanks again</p> 2012-04-23T18:53:04-04:004947276http://forums.asp.net/p/1796046/4947276.aspx/1?Re+ApiController+and+MVC+ControllerRe: ApiController and MVC Controller <p></p> <blockquote><span class="icon-blockquote"></span> <h4>IAsfour</h4> Can a razr view post requests to an API Controller (not through ajax)?</blockquote> <p></p> <p>Yes it can. In fact nothing stops you write an HtmlMediaTypeFormatter to implement this but it will probably be a lot of work. Your question is a valid one and is a common issue raised by the community. I can see these two to be unified perhaps in the next version but not in this version.</p> <p>But when it comes to the current version, you have to think if you are serving veiw or data: Use ASP.NET MVC to serve the views and use ASP.NET Web API to serve data,</p> <p></p> 2012-04-23T21:01:07-04:004947554http://forums.asp.net/p/1796046/4947554.aspx/1?Re+ApiController+and+MVC+ControllerRe: ApiController and MVC Controller <p></p> <blockquote><span class="icon-blockquote"></span> <h4>IAsfour</h4> What is not clear is the intended usage of the Web API Controller. &nbsp;Is its purpose mainly to create REST services? &nbsp;Can a razr view post requests to an API Controller (not through ajax)?</blockquote> <p></p> <p>See <a href="http://lostechies.com/jimmybogard/2012/04/10/asp-net-web-api-mvc-viewmodels-and-formatters/"> this </a>post.</p> <p></p> <p></p> <blockquote><span class="icon-blockquote"></span> <h4>aliostad</h4> In fact nothing stops you write an HtmlMediaTypeFormatter to implement this but it will probably be a lot of work</blockquote> <p></p> <p>Just wanted to make it clear that Media Type mainly depend upon mime type. This is why there is an built-in FormUrlEncodedMediaTypeFormatter(instead of HtmlMediaTypeFormatter).</p> <p></p> 2012-04-24T04:26:36-04:00