Stu1986C
Stu1986C

Reputation: 1510

Using Web API with HTML Pages

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.

Upvotes: 3

Views: 2365

Answers (1)

Carlos Grappa
Carlos Grappa

Reputation: 2381

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

Upvotes: 1

Related Questions