ASP. net mvc 3 (adding a controller) (2/9)

Source: Internet
Author: User
ArticleDirectory
    • Adding a controller
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 helloworldcontrollerBlock, 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 namedHelloworldcontrollerAnd 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.WelcomeMethod runs and returns the string "this is the welcome action method...". The default MVC mapping is/[Controller]/[actionname]/[parameters]. For this URL, the controller isHelloworldAndWelcomeIs 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 yourWelcomeMethod to include two parameters as shown below. Note that we 've ve used the C # optional-parameter feature to indicate thatNumtimesParameter 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 valuesNameAndNumtimes. 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.