Reputation: 1343
After reading a couple of Web Api 2 articles I decided to give it a try and use it in my current project because there is a lot of JSON data being send between the Views and the backend services. I'm making it a multipage application.
What I can't figure out is how to keep the routing from MVC and combine this with Web Api 2 to handle the data communication. Here are a couple of options that crossed my mind:
Creating 2 seperate projects: a MVC projects that handles to routing using controllers and returning views and a webapi projects that handles the data. (How will they both be accessible?)
Creating a MVC project and a submap with all the API's. (What about Separation of concerns?)
Creating a webapi project and make use of Area functionality where in MVC controllers are used to route.
Come to a conclusion that I better stick with MVC controllers to handel data communication for multipage applications.
Example solution structure:
Project.Core
Project.Services
Project.Data
Project...
Upvotes: 1
Views: 1114
Reputation: 12419
The WebAPI2 routing is separate from the MVC routing. The beauty of the API 2 routing is that you can make it whatever you want, completely independent of the controller and action name. Take the following example:
public class TriggerApiController : ApiController
{
private readonly ITriggerApiService _triggerApiService;
public TriggerApiController(ITriggerApiService triggerApiService)
{
_triggerApiService = triggerApiService;
}
[HttpGet]
[Route("api/v1/trigger/{externalID}")]
public async Task<IHttpActionResult> GetTrigger(Guid externalID)
{
var triggerApiModel = await _triggerApiService.GetAsync(externalID);
if (triggerApiModel != null)
{
return Ok(triggerApiModel);
}
return NotFound();
}
}
Important things to notice:
So for your use case, I would use a single web project, but put all the APIs into their own controllers (that extend ApiController) and specify a route for each API.
Upvotes: 2