mvc applcation

Overview of different part of an ASP.NET MVC application

What is MVC application?

After We create an ASP.NET MVC application visual studio, then go over to Solution Explorer, we are going to notice that we get a bunch of default folders, and some default files

We get a model folder, a views folder, and a controllers folder. So what’s going on these folders?

We get a default MVC application when we create a new MVC application in VS.

In an ASP.NET MVC application, a URL corresponds to something called a controller action. So we type in a URL, it’s not the page that requested from the server. It’s a particular method of a particular class get invoked. And so some application logic runs.
(in an ASP.NET web forms application, a URL corresponds to a page on disk. In other words, if we type in a URL into our browser and then hit enter, there better be a page that corresponds to that URL on your web server or we’ll get back a 404 error.)

How does a URL mapped over ASP.NET application?

That’s where something called URL routing comes in.
URL routing is responsible for mapping a URL to a particular controller action.
In Global.asax file contains all of the application life cycle events.One of those events is the application start events. That event is raised right when your application starts up.

We can notice that there a method called RegisterRoutes, it set up something called a route table for MVC application. What the route tables does is, it’s responsible for mapping URLs to particular controller actions.

AND we’ll notice that they give us a default route inside, we need to go to App_Start folder, then hit the RouterConfig.cs, its actually named Default.

So by default they’re going to assume that if you type in your URL, is going to have 3 parts. The first part is going to be the name of the controller, then action and ID. and they also gives us default values for these 3 parts.
So if we don’t type in the name of a controller, it’s going to default to Home, Index and empty screen.

What is controller?

In the controllers folder, we opened up and get the one controller by defaults in our sample application, the Home controller.
And we notice that it has functions inside of it. Index and About.

What is View?

Controller action retruns a view back from it.
The name of the view is inferred from the name of the controller action.
In RouterConfi.cs, there is a view called index, because that’s the name of the action, in a folder called home, because its the name of controller.
Expand out the view folder, we notice that there is a home subfolder that corresponds to the name of the controller.
Inside the home subfolder, we have 2 views called about and index, because we’re returning a view both from the index action and from the about action.
We opened up the view and we notice that it’s just HTML.it contains all of the content that you want to display to the user.

What is Model?

The idea of mode is, the model is supposed to consist of all your logic,all your application logic, all your database access logic… so the model consisits of a bunch of classes.
In controller, they should only contain the minimum amount of logic to be able to decide which view to show or what kind of response to perform in response to a browser request.
So you put all of your application logic, all of your data access stuff inside models folder. Then we can call out to those classes within controller method.
All the application logic in general should be pushed over into Models.

Summary:
1.Controller is responsible for doing is firing off some logic when a request is made from a browser.
2.A view contains the HTML mark up and contents that is return to the browser.
3.The model is consist of all the application logic.