How to avoid controller duplication/accountability in modern applications?

Asked

Viewed 52 times

1

Nowadays, communication between applications, mobile and/or other devices is increasing.

The . net provides the Asp.net MVC and Asp.net WebAPI (to date today, stable version).

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

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

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

1 answer

1

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

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

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

  • 1

    Thank you Gypsy :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.