[. NET object-oriented programming in depth] (6). NET MVC 6--basic operations for models, views, controllers, routes, etc.

Source: Internet
Author: User

[. NET object-oriented programming in depth] (6). NET MVC 6--basic operations for models, views, controllers, routes, etc.

1. Create a Web APP with visual Studio 2015

(1) file > New > Project, select Web>asp.net WEB application

(2) in the New Project Myfirstwebapp dialog box, select the ASP. 5 Template >web Application

Because it is the RC version, where the "Add unit Test" is temporarily unavailable, the above WebForms MVC Webapi will be merged, described in the previous section, so there is no need to select. Click OK to complete a MVC6 project creation.

(3) running the project

F5 run in debug mode, or Ctrl+f5 run in non-running mode

Tip: Launching the application Ctrl+f5 (non-debug mode) allows you to change the code, save the file, refresh the browser, and see the changes in the code. Many developers prefer to use non-debug mode to quickly launch applications and see changes.

The default item is a responsive page, and when the browser window shrinks or opens on a small screen device, you can see the navigation display as follows:

2. Project Structure

Let's take a look at the project structure differences between MVC6 and MVC5:

Here is the MVC5 project structure

Here is the MVC6 project structure

Yes, the project structure is significantly different compared to MVC5

(1) Project document becomes Xproj (MVC5 project file is csproj)

(2) Program configuration file becomes Config.json (MVC5 is web. config)

(3) Added Project.json is mainly used for project configuration, responsible for assembly, project deployment settings, some functions similar to MVC5 package.config.

(4) Package.json This file is the NPM configuration file in MVC6, based on the Nodejs Package manager.

(5) Gulpfile.js is a gulp configuration file, Gulp is a Nodejs-based JavaScript Task Manager that primarily manages content in NPM and bower in ASP.

(6) Stratup.cs program start entry, similar to the original Global.asax

(7) Project_readme.html project documentation, no specific role.

(8) Wwwroot static resources files (such as CSS,IMAGES,JS, etc.) of the storage directory

(9) Dependencies Bower and NPM are dependent management packs.

(10) References assembly reference, similar to the previous one, but now has version differentiation (such as ASP. NET 5.0 and ASP. NET Core 5.0)

To learn more about the MVC6 project structure of friends, you can refer to the article: http://www.cnblogs.com/TomXu/p/4496407.html

3. Adding controllers (Controller)

In the project folder controllers right click, select Add > New Item

In the dialog box that opens, select the MVC controller class, named HelloWorldController.cs

We opened the controller we just created HelloWorldController.cs changed to the following:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Threading.Tasks;usingMICROSOFT.ASPNET.MVC;//for more information on enabling MVC for empty projects, visithttp://go.microsoft.com/fwlink/?LinkID=397860namespacemyfirstwebapp.controllers{ Public classHelloworldcontroller:controller {//GET:/helloworld/         Public stringIndex () {return "This is my default action ..."; }        //         //GET:/helloworld/welcome/         Public stringWelcome () {return "I am a welcome method ..."; }    }}
View Code

Let the above method return a string, F5 run, and then add HelloWorld after the address, refresh the browser, the effect is as follows:

We open the Startup.cs file and look at the routing

First find the following code:

The route is set to the default controller =home, which defaults to the Index parameter as the default ID

The routing format is:/[controller]/[actionname]/[parameters]

We specify the controller above, then open the default method index, if we specify the method, run the result as follows:

Then let's take a look at the parameters:

Add the following method to HelloWorldController.cs

   Public string Welcome2 (stringint1)  {                       return  HtmlEncoder.Default.HtmlEncode (                "hello,name:"", ID:  " + ID);}

Note: The code used above Htmlencoder.default.htmlencode protects the application from malicious input (such as JavaScript).

ID has a default value, when we pass in the parameter ID, the default display is 1

Pass in two parameters:

4. Adding Views (view)

(1) First create a folder with the same name as the controller in the project's view directory HelloWorld

For example: The view name is HelloWorld, then the corresponding controller name is: Helloworldcontroller

(2) Create a view in the View folder HelloWorld named: viewshow.cshtml,

Replace the view file as follows:

@{    viewdata["Title"] = "Index";} 

And in Helloworldcontroller, add the same name method, the code is as follows:

 Public Iactionresult viewshow () {      return  View ();}

Then run the address as shown:

Controller method View () returns the view

5. Add a model (modle)

Model add is actually adding a class file, here simply create an entity, followed by a complete description

Right-click the project folder models, add a class file named: Movie.cs

The contents are as follows:

 Public classmovie{ Public intID {Get;Set; }  Public stringTitle {Get;Set; }  PublicDateTime ReleaseDate {Get;Set; }  Public stringGenre {Get;Set; }  Public decimalPrice {Get;Set; }}

Here a model is created to complete.

6. Summary:

This article is very easy to understand, introduced the MVC6 Basic controller, view, model creation and the simplest routing settings, although the foundation, but these are we learn the basis of MVC, will continue to introduce the MVC Connection database and implementation of ADD, delete, change, check and so on.

7. Full Source code Download

Git address: https://github.com/yubinfeng/BlogExamples

==============================================================================================

Back to Catalog

< If it is helpful to you, please remember to click on the recommendation Oh, if there is have no understanding or error the Place , please communicate more >

< for those who have difficulty reading this series of articles, please read ". NET object-oriented programming fundamentals" and ". NET object-oriented programming advanced " >

< REPRINT statement: Technology needs to share the spirit, welcome to reprint the article in this blog, but please specify copyright and url>

. NET Technology Exchange Group: 467189533

==============================================================================================

[. NET object-oriented programming in depth] (6). NET MVC 6--basic operations for models, views, controllers, routes, etc.

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.