Jack Shepherd
Jack Shepherd

Reputation: 393

Separating APIs from MVC project

I'm trying to get into ASP.NET Core. A lot of the examples out there have the front end and the back end using the same project. What I'd like to do is to have one API project, and one front end project. I prefer this separate solution because it'll be easier for me to add on a mobile component in future if needed.

My question is this: Assuming I have projects, one Web API (core) and an MVC (core) would I be having duplicate controllers?

E.g., in the API project, I would have the following controllers

  1. LoginController
  2. MembersController
  3. ShopController

Would that mean in my MVC project, I'll have the same controllers, and within the controllers I'll be making REST calls to the corresponding controllers in my API project? Would it seem redundant and inefficient to have each request go through the MVC controller, and then to the API controller?

How should the calls be secured? Would I be issuing a JWT upon a successful login against the API project to protect subsequent calls to the API layer?

Upvotes: 2

Views: 1543

Answers (1)

Mithun Pattankar
Mithun Pattankar

Reputation: 1372

Jack,

I could see in this way, controller names (mvc & api) are same with a different purpose. MVC controllers will render UI to work with client side stuffs where as API controllers will work with data. There is no redundancy I see in it.

I suggest keeping API & MVC separately (don't worry about names). If API separate then you can have mobile based UI too, just by consuming the api's

From MVC you can either call these API from client side or server side. This actually will decouple MVC & API.

In API's project, have login method (verifies & generates token); this token can be used in MVC or mobile by login page. Then use the token to call other remaining API methods (bearer token style).

Split data access & expose its methods separately in project, use Web API to make it public. You never know if you need them in non-MVC apps.

Upvotes: 3

Related Questions