Normal controller are already able to handle json, and not only Html. The difference is not on this, but on the different way the two controllers can be programmed. Wit Api controllers the result can be negotiated with the caller and we have a simple programming
model of how to build the full Http response(Http headers included). In a few words WebApi are less specialized and more flexible. Normal controllers are for sure preferred for creating normal Html Content throught Views, but WebApi are also able to create
Html without the View mechanism
WebApi can be used also without Mvc. Just google "WebApi without Mvc" to find how ro use with web forms and as a self hosted server.
Authentication works as in normal asp,net web sites: using web.config settings. Moreover there is also an Authorize attibute like with normal controller.
with json requests, the major difference between webapi and standard controller, is how the json is processed. with webapi, the json is deserialized and must match the contract json (model). in the standard controller, the json is convereted to name.values
pairs then bound to the model. this is a much more forgiving and allows extra properties or missing properties. with the standard contoller you could a parameter as part of the json data.
take this example:
ActionResult processJson(int id, myModel obj)
with webapi, id must be a route value, while with the standard controller, it could be part of the json data.
bruce (sqlwork.com)
Marked as answer by Angie xu - MSFT on Aug 30, 2012 05:30 AM
Point 1 and 2 are well answered, I will just add a few comments on #3
3. If you host your web api on IIS, it will inherit the default asp.net IIS security (forms or windows etc). If you do a self host, you have take care of authentication and authorization. To implement this kind of security watch the video in
http://ndcoslo.oktaset.com/Agenda click Thursday and search for securing web api
Please don't forget to mark this post as answered incase if this worked for you
2 - Can I create a Web API in a different Project from my MVC4 project?
Should this be a on Web API project or a Class Library?
Where should the routes of the Web API be defined? In the Web API project or in the MVC 4 project?
How can then access the Web API actions from the MVC 4 project?
Yes you can create a separate Web API project
Web API project is cooked up over class libary project. You can start up with class library and add the reference manually
I am not sure, but I believe that the routes are to be defined on the Web API project and our api cannot be accessed until you host it separately in IIS or self host
Once you host the api separately in IIS or self host, you can access it normally using the routes defined in the web api project
Please don't forget to mark this post as answered incase if this worked for you
I'm working on a large ASP.NET MVC/Web API project and wanted to separate out the controllers into their own project (as described in this article http://msdn.microsoft.com/en-us/magazine/jj190803.aspx). The difference is that I'm needing to separate out
the ASP.NET Web API controllers, not “normal” MVC controllers.
In my solution, I have two separate projects:
One ASP.NET MVC 4 project for serving up HTML/CSS/JavaScript (note I'm not using any standard MVC controllers, this project is all client/browser-side code that makes jQuery/Ajax calls to the Web API)
One ASP.NET Web API project (this project is only the ApiController(s), no views, HTML, etc., I'm still wanting the Web API project to be hosted in IIS, not self-hosted)
Anyway, I’ve seen other posts and such that haven’t really explained my exact situation, and I'm having trouble getting this solution working.
How can I break out my Web API controllers into their own separate project and use them from my HTML/JavaScript code in my separate MVC project? And, how do I call the API’s endpoints from my JavaScript/jQuery code in the separate MVC project?
WRT point 2 - I m implementing web api in ASP.NET web form then do I need to create a controller derived from ApiController? Can you pls share an article & example, it will be very helpful. Thank you
WRT point 2 - I m implementing web api in ASP.NET web form then do I need to create a controller derived from ApiController? Can you pls share an article & example, it will be very helpful. Thank you
Yes WebAPI should inherit from ApiController. You can learn about WebAPI here
http://www.asp.net/web-api
shapper
Contributor
3932 Points
3789 Posts
A few questions on Web API and MVC 4
Aug 23, 2012 09:45 AM|LINK
Hello,
I have been checking Web API and MVC 4 and I have a few questions:
1 - Why having an API for JSON, XML, ... formats and a standard controller for HTML?
Shouldn't the API and Standard Controllers be merged into one which would return any data type?
2 - Can I create a Web API in a different Project from my MVC4 project?
Should this be a on Web API project or a Class Library?
Where should the routes of the Web API be defined? In the Web API project or in the MVC 4 project?
How can then access the Web API actions from the MVC 4 project?
3 - How to handle authentication in Web API?
Let's say I access an action which returns a JSON result.
How to do authentication? In the action itself?
This API would be used by external systems.
Thank You,
Miguel
francesco ab...
All-Star
20888 Points
3277 Posts
Re: A few questions on Web API and MVC 4
Aug 24, 2012 10:17 AM|LINK
Mvc Controls Toolkit | Data Moving Plug-in Videos
bruce (sqlwo...
All-Star
36656 Points
5438 Posts
Re: A few questions on Web API and MVC 4
Aug 24, 2012 03:28 PM|LINK
with json requests, the major difference between webapi and standard controller, is how the json is processed. with webapi, the json is deserialized and must match the contract json (model). in the standard controller, the json is convereted to name.values pairs then bound to the model. this is a much more forgiving and allows extra properties or missing properties. with the standard contoller you could a parameter as part of the json data.
take this example:
ActionResult processJson(int id, myModel obj)
with webapi, id must be a route value, while with the standard controller, it could be part of the json data.
syed.iddi
Participant
1688 Points
363 Posts
Re: A few questions on Web API and MVC 4
Aug 24, 2012 03:45 PM|LINK
Point 1 and 2 are well answered, I will just add a few comments on #3
3. If you host your web api on IIS, it will inherit the default asp.net IIS security (forms or windows etc). If you do a self host, you have take care of authentication and authorization. To implement this kind of security watch the video in http://ndcoslo.oktaset.com/Agenda click Thursday and search for securing web api
syed.iddi
Participant
1688 Points
363 Posts
Re: A few questions on Web API and MVC 4
Aug 24, 2012 03:49 PM|LINK
Yes you can create a separate Web API project
Web API project is cooked up over class libary project. You can start up with class library and add the reference manually
I am not sure, but I believe that the routes are to be defined on the Web API project and our api cannot be accessed until you host it separately in IIS or self host
Once you host the api separately in IIS or self host, you can access it normally using the routes defined in the web api project
lmttag
Member
11 Points
54 Posts
Re: A few questions on Web API and MVC 4
Oct 15, 2012 03:19 PM|LINK
I'm working on a large ASP.NET MVC/Web API project and wanted to separate out the controllers into their own project (as described in this article http://msdn.microsoft.com/en-us/magazine/jj190803.aspx). The difference is that I'm needing to separate out the ASP.NET Web API controllers, not “normal” MVC controllers.
In my solution, I have two separate projects:
Anyway, I’ve seen other posts and such that haven’t really explained my exact situation, and I'm having trouble getting this solution working.
How can I break out my Web API controllers into their own separate project and use them from my HTML/JavaScript code in my separate MVC project? And, how do I call the API’s endpoints from my JavaScript/jQuery code in the separate MVC project?
Thanks.
Bibin.Philip
Member
4 Points
3 Posts
Re: A few questions on Web API and MVC 4
Feb 01, 2013 01:19 PM|LINK
Hi,
WRT point 2 - I m implementing web api in ASP.NET web form then do I need to create a controller derived from ApiController? Can you pls share an article & example, it will be very helpful. Thank you
CodeHobo
All-Star
18647 Points
2647 Posts
Re: A few questions on Web API and MVC 4
Feb 01, 2013 07:09 PM|LINK
Yes WebAPI should inherit from ApiController. You can learn about WebAPI here
http://www.asp.net/web-api
Blog | Twitter : @Hattan
Bibin.Philip
Member
4 Points
3 Posts
Re: A few questions on Web API and MVC 4
Feb 04, 2013 05:31 AM|LINK
Thank you very much.