Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft ASP .NET Web API, MVC 4 and SPA Architecture

Microsoft recently released MVC 4 Beta, which has these new very nice features like Web API and SPA. And as always Microsoft's demos do not demonstrate best practices from software design prospective. For example, using DbController which is tightly coupled to EF.

It seems for me that SPA and Web API go hand-by-hand in modern ASP .NET app. I would like to hear any suggestions about structuring MVC 4-based solution, which is going to apply these new technologies like Web API and SPA.

For example, is it a good practice to separate Web API project with it's own controllers out of base MVC4 project or not. How to deal with SPA and not to use DbController in order to keep data persistence separately? What's going to be a main role of regular MVC4 app and especially Razor views?

Any other thoughts or suggestions are highly appreciated.

like image 888
Alex Avatar asked Mar 10 '12 01:03

Alex


People also ask

What is ASP.NET MVC 4 web application?

ASP.NET MVC 4 is a framework for building scalable, standards-based web applications using well-established design patterns and the power of the ASP.NET and the . NET framework.

Is ASP Net Web API and ASP.NET MVC same?

Asp.Net Web API VS Asp.Net MVC Asp.Net MVC is used to create web applications that return both views and data but Asp.Net Web API is used to create full-blown HTTP services with an easy and simple way that returns only data, not view.

What is Spa in MVC?

A Single Page Application (SPA) is a web application that fits on a single web page. In this type of application, the server provides static HTML views, CSS and JavaScript and the application then loads data by making Ajax calls to the server. All subsequent views and navigation occurs without a postback to the server.


2 Answers

On separation of MVC4 + Web API: imho (as always) it depends on your concrete project.

Regarding EF: you should definitely not return EF Entities but return your own DTOs instead.

The role of MVC razor views could be rendering partial views you dynamically load from the client. You also could do some stuff like conditional loading of CSS / JS etc. for the Index page being loaded initially.

like image 69
Alexander Zeitler Avatar answered Oct 21 '22 21:10

Alexander Zeitler


I think it's a good idea to keep the API in a separate web site project from your SPA/web site, as you can run in to problems with greedy routes.

Definitely keep your data access separate and loosely coupled.

like image 22
Antony Scott Avatar answered Oct 21 '22 21:10

Antony Scott