ArticleDirectory
Adding a controller
MVC standsModel-View-Controller. MVC is a pattern for developing applications such that each part has a separate responsibility:
- Model: The data for your application.
- Views: The template files your application will use to dynamically generate HTML responses.
- Controllers: classes that handle incoming URL requests to the application, retrieve model data, and then specify view templates that render a response to the client.
We'll be covering all these concepts in this tutorial and show you how to use them to build an application.
Create a new controller by right-clickingControllersFolder inSolution ExplorerAnd then selectingAdd Controller.
Name your new controller "helloworldcontroller" and clickAdd.
Notice inSolution ExplorerOn the right that a new file has been created for you namedHelloworldcontroller. CSAnd that the file is open in the IDE.
Inside the newPublic class helloworldcontroller
Block, create two new methods that look like the following code. We'll return a string of HTML directly from the Controller as an example.
Using System . Web . MVC ; Namespace Mvcmovie . Controllers { Public Class Helloworldcontroller : Controller { Public String Index () { Return "This is my <B> default </B> action ..." ; } Public String Welcome () { Return "This is the welcome action method ..." ; } } }
Your controller is namedHelloworldcontroller
And your new method is namedIndex
. Run the application (Press F5 or Ctrl + F5). Once your browser has started up, append "helloworld" to the path in the address bar. (on my computer, it'sHTTP: /localhost: 43246/helloworld) Your browser will look like the screenshot below. In the method above, the Code returned a string directly. We told the system to just return some HTML, and it did!
ASP. net MVC invokes different controller classes (and different action methods within them) depending on the incoming URL. the default mapping logic used by ASP. net MVC uses a format like this to control what code is invoked:
/[Controller]/[actionname]/[parameters]
the first part of the URL determines the Controller class to execute. so /helloworld maps to the helloworldcontroller
class. the second part of the URL determines the Action Method on the class to execute. so /helloworld/index wocould cause the index
method of the helloworldcontroller
class to execute. notice that we only had to browse to /helloworld and the index
method was used by default. this is because a method named index
is the default method that will be called on a controller if one is not explicitly specified.
BrowseHttp: // localhost: xxxx/helloworld/welcome.Welcome
Method runs and returns the string "this is the welcome action method...". The default MVC mapping is/[Controller]/[actionname]/[parameters]
. For this URL, the controller isHelloworld
AndWelcome
Is the action method. We haven't used[Parameters]
Part of the URL yet.
Let's modify the example slightly so that we can pass some parameter information from the URL to the Controller (for example,/Helloworld/welcome? Name = Scott & numtimes = 4). Change yourWelcome
Method to include two parameters as shown below. Note that we 've ve used the C # optional-parameter feature to indicate thatNumtimes
Parameter shocould default to 1 if no value is passed for that parameter.
Public String Welcome ( String Name , Int Numtimes = 1 ) { String Message = "Hello" + Name + ", Numtimes is :" + Numtimes ; Return "" + Server . Htmlencode ( Message ) + "" ; }
Run your application and browseHttp: // localhost: xxxx/helloworld/welcome? Name = Scott & numtimes = 4. You can try different valuesName
AndNumtimes
. The system automatically maps the named parameters from your query string in the address bar to parameters in your method.
In both these examples the controller has been doing the VC portion of MVC-that is, the view and controller work. the Controller is returning HTML directly. ordinarily we don't want controllers returning HTML directly, since that becomes very cumbersome to code. instead we'll typically use a separate view template file to help generate the HTML response. let's look next at how we can do this.