Tuesday 8 December 2020

Asp.Net MVC Architecture and Routing

 


Asp.Net MVC is web based framework provided by Microsoft with dotnet framework. MVC is architectural pattern and makes you application fast, secure and robust.


MVC Architectur

MVC (Model View Controller) separates application into Model View controller which is different from traditional asp.net web forms.


Model : Mock classes of data, like for Employee table in database in model represent as class and column name of employee table here represent as property of Employee class.


View : View is User interface where you can decide how your data shown on browser.


Controller : Controller contains business logic and Data Logic (binding database data into mock classes) and return data to View. Controller is also handles http request (routing mechanism in MVC)


You can see below URL form with controller and action method inside controller.

Home is controller name and Index is action method inside home controller. URL formed with controller and action method in MVC.





Http Request come to controller, controller use model properties and prepare data, controller send data to view and view organize UI with help of model and return response.


Routing In MVC :

According to microsoft, routing is important feature introduced in MVC. It is responsible for mapping incoming http request with controller and action method name. Routing is already built-in and configured when you create new MVC application with default route. You can also create routes based on requirement.

Lets create simple application in MVC and understand how routing works in MVC application.


 



Open Global.asax.cs file, in Application_start method, all application level settings configured here. MVC route is also configured here highlighted

RouteConfig.RegisterRoutes(RouteTable.Routes);


 


RouteConfig : class available in App_start folder, select RouteConfig and press F12 to goto RouteConfig class, you will see below code. MVC crreate route with name by default.


For our application if requirement to create more than one route we can create routes in RouteConfig class with the help of MapRoute method.


When application start based on number of routes configurd in RegisterRoutes method of RouteConfig, route table gets created and keep track of routes configured for an application and used when incoming request from browser.


0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home