How to avoid duplication of controllers / responsibility in modern applications?

1

Today, communication between applications, mobile and / or other devices is growing.

.NET provides Asp.net MVC and Asp.net WebAPI (to date, stable version).

Commonly Asp.net MVC returns Views , although WebApi also returns is not correct. The most correct way to use an API would be data only ( json )

So how do I avoid duplicating Controllers in cases where I also need to communicate with mobile and aplicação web ? Knowing that both methods will have CRUD(Create/Read/Update/Delete) implemented

I know that Single Page Application(SPA) is an option to reuse the same Controller Api, but what if we are not using SPA ?

    
asked by anonymous 28.10.2015 / 20:45

1 answer

1

In the case of ASP.NET MVC5, where it is impossible to make the Controller serve more than one type of information format, the most appropriate approach is to write an intermediate layer that processes the request all by creating two groups of Controllers , one for requests that return HTML and another that returns requests in JSON, each with its route table.

Therefore, in the case of MVC5, duplication of Controllers is not preventable.

For the ASP.NET MVC6 case, there is no need for this separation because the Controller is the same for any information format. As of the date of this response, the implementation is already unified, but so far I have not found a simple way for the application to identify which server is not based on route configuration.

    
28.10.2015 / 21:01