2

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?

3
  • 1
    Why can't your MVC view call REST JSON endpoint directly from javascript? if you keep API in same project, it will be lot easier to avoid Cross origin issues, duplicate tunneling request from MVC controller to API controller and most likely you can keep views as plain html and create entire web app by calling Json endpoints. Unless you are going to hit million users and you will need quick scalability, approach of separating API from MVC is useless. Not every application has to be divided in too many components, you have to evaluate your budget, target audience and growth plan. Commented Aug 23, 2017 at 8:53
  • Hi Akash, thanks for the advice. what if I need to have a mobile app, on top of my MVC app that calls my API project. Would having only 1 MVC project still be viable? Commented Aug 23, 2017 at 9:19
  • Yes, you can call all API calls from mobile even if it is part of same MVC project. Commented Aug 23, 2017 at 9:53

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.