DEV Community

Kasun Nanayakkara
Kasun Nanayakkara

Posted on • Edited on

.NET MVC Architecture

MVC describes the Model-View-Controller architecture.

Model describes the data layer. The data layer contains data attributes, as well as getter and setter methods. The data can be defined using specific behaviors, such as DisplayName, Range, Required, etc.
The data model can also describe the functionality related to the data structure, such as data retrieval, updates, and deletion functions. Some models manipulate data into another format.

View describes the user interface that presents the data to the user. It can include elements like textboxes, dropdowns, buttons, tables, and other UI components.

Controller describes the API and control methods for retrieving the data. It communicates using routing methods. There are several methods to route to the API. The default route is ‘Api/Controller/Action/id’, but the user can define their preferred routing method. For example,

• /api/contacts
• /api/contacts/21
• /api/products/apple
Enter fullscreen mode Exit fullscreen mode

Can an API use HTTP verbs?

Yes, an API can use HTTP verbs such as [HttpGet], [HttpPost], [HttpPut], [HttpDelete], etc.
Additionally, the user can define the action name using the [ActionName("name")] attribute. This allows the user to hide the mechanism of the action. For example, the user can define an API route as Api/enroll/enrollment/1.

[RoutePrefix("enroll")] 
public class EnrollmentConroller: APIController(
[HttpPost]
[ActionName("enrollment")]
public void getEnrollment(int id){}
Enter fullscreen mode Exit fullscreen mode

Router attributes can be added to manipulate the data directly.

[Route("patient /{EnrollmentID}/enrollment")]
[HttpGet]
public IEnumerable<Enrollment> get enrollment (int EnrollmentID) { }
[Route("patient / enrollment ")]
[HttpPost]
 public HttpResponseMessage updateEnrollment(Enrollment obj) { }
Enter fullscreen mode Exit fullscreen mode

Lifecycle of the MVC

  • User request ->
  • Fetch the routing table ->
  • Execute router ->
  • Controller initiate ->
  • Execute action ->
  • Execute/Map result ->
  • view engine maps the result to UI ->
  • Retrieve data / request

Top comments (0)