3

I've been using ASP.Net MVC (with Razor syntax) successfully for over a year now and wanting to move to Web API.

With MVC I'd usually set my n-tier architecture up as follows (simplified for ease of explanation):

  1. Data (Class Library)
  2. Repository (Class Library)
  3. Services (Class Library)
  4. Presentation (MVC)

What I am starting to dislike about this is that there seems to be a lot going on in the Presenation layer; you've got models, razor views, controllers plus any added niceties such as javascript / AngularJS & CSS.

How would you recommend I set up my tiers when using Web API instead of MVC? Is it the same as above but with an added layer? Something like this:

  1. Data (Class Library)
  2. Repository (Class Library)
  3. Services (Class Library)
  4. Web API
  5. Presentation (HTML + javascript + CSS)

Or are all the HTML pages to be put in the same project as the Web API? This seems odd to me as I feel like you need to keep your API separate from all other concerns such as the User Interface.

Any insight will be gratefully received.

3
  • 3
    the general idea is the WebApi and the front-end piece would be entirely separate entities. Commented Jul 13, 2014 at 21:48
  • Ah ok, I think I get what you mean. Along the lines of I'd have my usual n-tier application (substituting MVC layer fro Web API) and then outside of my solution I'd have any HTML pages that I wish to serve? Commented Jul 14, 2014 at 5:35
  • 1
    yeah, the benefit of the WepAPI is that you don't care what type of client consumes it. A Desktop App, Mobile App, and Website all integrate the same way. At my work we use WebAPI and an angular front-end, and that works very well Commented Jul 14, 2014 at 12:09

1 Answer 1

1

I'm working along those lines these days. Web API as a gateway for data from and to the application, and HTML completely independent. Now here you got two choices

  1. Use HTML pages and with AJAX you interact with Web API, the only big issue i can think of is routing and url rewriting.
  2. Use MVC views, but as HTML pages, that means no model classes,no controller methods (other than the one that serves the view). Basically all you use is the builtin routing and maybe the master page.

That on the front-end side, as for the backend the Web API layer is what in older applications would have been a WCF application, so the Web API controlles is what the .svc file implementation was, and from there you'd have a class library for business logic, one for data access (SQL / ORM) and finally another for domain objects (shared across web api, business and data layers).

Thats how I tend to structure things whenever I can. For using HTML with Web API, I tend to like option 2, mostly because is easily integrated with VS, and if you really need anything from Razor you have it handy. And mostly because that way you work with the structure Visual Studio favors and not against it (and in the long run its easier to go along with it)

Hope that helps

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.