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.
Do I have the general idea right here or am I completely wrong?
Looking for some input since documentation is kind of scarce out there.
No, the two use different base classes that serve different purposes. You can see the difference just by looking at the types,
MVC Controller and
ApiController.
Note 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 ApiController class
instead of Controller class. The first major difference you will notice is that actions on Web API controllers do not return views, they return data.
Darrell Norton, MVP
Darrell Norton's Blog Please click "Mark as Answer" if this helped you.
I do understand that they are different types altogether but I am wondering about the general usage of each. 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. That
is all clear to me. What is not clear is the intended usage of the Web API Controller. Is its purpose mainly to create REST services? Can a razr view post requests to an API Controller (not through ajax)?
Can a razr view post requests to an API Controller (not through ajax)?
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.
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,
What is not clear is the intended usage of the Web API Controller. Is its purpose mainly to create REST services? Can a razr view post requests to an API Controller (not through ajax)?
In fact nothing stops you write an HtmlMediaTypeFormatter to implement this but it will probably be a lot of work
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).
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
IAsfour
0 Points
2 Posts
ApiController and MVC Controller
Apr 23, 2012 06:27 PM|LINK
I have more of a general question.
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.
Do I have the general idea right here or am I completely wrong?
Looking for some input since documentation is kind of scarce out there.
Thanks
mvc
DarrellNorto...
All-Star
86805 Points
9644 Posts
Moderator
MVP
Re: ApiController and MVC Controller
Apr 23, 2012 06:45 PM|LINK
No, the two use different base classes that serve different purposes. You can see the difference just by looking at the types, MVC Controller and ApiController.
Here's a great quote from this page:
Note 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 ApiController class instead of Controller class. The first major difference you will notice is that actions on Web API controllers do not return views, they return data.
Darrell Norton's Blog
Please click "Mark as Answer" if this helped you.
IAsfour
0 Points
2 Posts
Re: ApiController and MVC Controller
Apr 23, 2012 06:53 PM|LINK
Thanks for your reply.
I do understand that they are different types altogether but I am wondering about the general usage of each. 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. That is all clear to me. What is not clear is the intended usage of the Web API Controller. Is its purpose mainly to create REST services? Can a razr view post requests to an API Controller (not through ajax)?
Thanks again
aliostad
Member
228 Points
55 Posts
Re: ApiController and MVC Controller
Apr 23, 2012 09:01 PM|LINK
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.
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,
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: ApiController and MVC Controller
Apr 24, 2012 04:26 AM|LINK
See this post.
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).
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD