Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding controllers in MVC

Tags:

asp.net-mvc

I'm building a site similar to StackOverflow, mostly as a learning exercise, and I'm having difficulty understanding how to decide on the different controllers in the MVC pattern.

What exactly is a controller? What controllers would you use to model a Q&A website similar to SO? I'm using ASP.Net MVC, and I notice the URL pattern is always "/Controller/Action" - but that is definitely not how I'd like the final URLs to look like ("/Question/123" does not fit into that pattern). Is that a consideration?

I know this is actually a mix of several questions ... perhaps what I really need is a good tutorial to understand the basics.

like image 341
ripper234 Avatar asked Jan 01 '10 19:01

ripper234


People also ask

How does MVC know which controller to use?

Also, MVC relies heavily on reflection, which allows you to inspect types at runtime using strings. Reflection is used in many programming frameworks.

Can you explain model controller and view MVC?

-MVC is an architectural pattern consisting of three parts: Model, View, Controller. Model: Handles data logic. View: It displays the information from the model to the user. Controller: It controls the data flow into a model object and updates the view whenever data changes.


1 Answers

In simple words, a controller is a contract/bridge between a model and the view.

Here is the flow:

A controller is used for main request processing logic. If a page has to talk with database, the controller sends a request to the model, model performs its task with db and returns some response or db records back to the controller then controller sends this response to the view.

The below picture explains the process more easily:

alt text
(source: shopno-dinga.com)

like image 148
Sarfraz Avatar answered Oct 16 '22 01:10

Sarfraz