Learn from ASP. NET MVC II: The first ASP. NET MVC Program

Source: Internet
Author: User

Summary:

This article takes you one step at a to create a simple ASP. NET MVC program.

Create a new ASP. NET MVC Project

After clicking the "OK" button, open the following window:

Here you select the "Empty" template and the "MVC" option. No unit tests are created this time, so "ADD unit tests" is not selected. Click on the "OK" button.

After the project is created, the project's default folder structure is this:

Click Run to open the browser when the program executes:

The result of the execution is a server error in the application because we created an empty ASP. NET MVC project with no controllers and no views.

Add a first controller

Select Controller folder, right mouse button, select "Controller" in the popup menu. Open the following dialog box:

Select "MVC controller-empty" and click the "Add" button.

In the dialog box that pops up, modify the controller name "HomeController" and add the empty controller to the project.

The default controller code is this:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingsystem.web;5 usingSYSTEM.WEB.MVC;6 7 namespacepartyinvites.controllers8 {9      Public classHomecontroller:controllerTen     { One         //Get:home A          Publicactionresult Index () -         { -             returnView (); the         } -     } -}

Modify action Index to return a string:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingsystem.web;5 usingSYSTEM.WEB.MVC;6 7 namespacepartyinvites.controllers8 {9      Public classHomecontroller:controllerTen     { One         //Get:home A          Public stringIndex () -         { -              return "Hello Word";  the         } -     } -}

Execute the program and get the running result in the browser:

Back to Web page

Continue to modify the index method and return the result using the view () function.

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingsystem.web;5 usingSYSTEM.WEB.MVC;6 7 namespacepartyinvites.controllers8 {9      Public classHomecontroller:controllerTen     { One         //Get:home A          PublicViewResult Index () -         { -             returnView (); the         } -     } -}

Executes the program and gets the execution results in the browser.

This error is due to the fact that we have not yet added a view to the Controller's action.

Right-click inside the index method and select the "Add View" button in the pop-up menu.

Click the "Add" button in the dialog box that pops up.

The default content of the created view is the following:

1 @{2 Layout = null;3 }4 5 <!DOCTYPE HTML>6 7 <HTML>8 <Head>9     <Metaname= "Viewport"content= "Width=device-width" />Ten     <title>Index</title> One </Head> A <Body> -     <Div>  -     </Div> the </Body> - </HTML>

Modify the content within <div></div>.

1 @{2 Layout = null;3 }4 5 <!DOCTYPE HTML>6 7 <HTML>8 <Head>9     <Metaname= "Viewport"content= "Width=device-width" />Ten     <title>Index</title> One </Head> A <Body> -     <Div>  - Hello, World the     </Div> - </Body> - </HTML>

Executes the program and gets the execution results in the browser.

The file index.cshtml is added to the folder Views->home at this time.

Add Dynamic output

Returns the Index method, which outputs dynamic content.

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingsystem.web;5 usingSYSTEM.WEB.MVC;6 7 namespacepartyinvites.controllers8 {9      Public classHomecontroller:controllerTen     { One         //Get:home A          PublicViewResult Index () -         { -              int hour = System.DateTime.Now.Hour;  the             viewbag.greeting = Hour <  ? " Good moring ": "Good afternoon";   -              return  View ();  -         } -     } +}

Returns the index view, which renders dynamic content.

1 @{2 Layout = null;3 }4 5 <!DOCTYPE HTML>6 7 <HTML>8 <Head>9     <Metaname= "Viewport"content= "Width=device-width" />Ten     <title>Index</title> One </Head> A <Body> -     <Div>  - @ViewBag. Greeting World (from the view) the     </Div> - </Body> - </HTML>

Executes the program and gets the running result in the browser.

  

Learn from ASP. NET MVC II: The first ASP. NET MVC Program

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.