First, ASP. net mvc 3.0 [Hello world !]

Source: Internet
Author: User

Before writing Hello world with ASP. net mvc, let's talk about what MVC is!

MVC is a software architecture model. It divides a software system into three parts: model, view, and controller ). The purpose of the MVC pattern is to implement a dynamic program design, simplify subsequent modifications and extensions of the program, and make reuse of a part of the program possible. In addition, this mode simplifies the complexity to make the program structure more intuitive. The software system also gives the functions of each basic part through the separation of its basic parts.

Model, view, and controller (MVC). The relationship between them is as follows: 1.

Figure 1.

Model)The data model is used to encapsulate data related to the application's business logic and to process the data. The "model" has the right to directly access data, such as accessing databases. "Model" does not depend on "View" and "controller", that is, the model does not care about how it is displayed or operated. However, data changes in the model are generally published through a refresh mechanism. To implement this mechanism, the views used to monitor this model must be registered on this model in advance, so that the views can understand the changes in the data model.

View)The view layer can display data purposefully; in the view, there is generally no program logic. To refresh a view, the view needs to access the data model it monitors. Therefore, you should register the data that it monitors in advance.

Controller)The Controller acts as an organizational unit between different layers and is used to control application processes. It processes events and responds. "Events" include user behaviors and changes in data models.

Therefore, figure 1 can be extended to the form of 2.

Figure 2.

Here is the description of MVC! Let's say hello to everyone! Since its launch, MVC has also undergone n transformations! Version 1.0 has too many bugs and is relatively immature and unstable. At that time, there will be a lot of problems, but there will still be some deficiencies, there are still a lot of problems, first of all, the page is full of <%> makes many people feel uncomfortable! By 3.0, it would be much more perfect. A new razor engine writing method is much more friendly to programmers! If you are a newbie, I suggest learning from 3.0! Of course, there is also the expected version 4.0, which is still in Beta status, which is more powerful than version 3.0.

Of course, I would like to thank many people who wrote a lot of articles for us to learn from. net MVC still has little information, which shows the painstaking efforts of the predecessors on the garden to let us learn new things. Thank you very much.

Create a project ASP. net mvc 3 web application, 3.

Figure 3.

After you click Create, enter the option box shown in Figure 4.

Figure 4.

Of course we chose the view engine as the razor engine, because we only need it to give us a simple sentence, you do not need to create a unit test project (this is required for a large project ). Create an empty project. After the project is created, vs automatically configures the environment for us, as shown in Figure 5.

Figure 5.

After the creation is complete, it must be 404. Because the project is empty, we didn't add anything to it.

What is requested in MVC?ControllersIn ASP. in net MVC, controllers is a simple C # class (usually inherits system. web. MVC. controller base class), each public method in controllers is called Action method, which means you can call it through URL. MVC Conventions: Place All controllers in a folder named controllers (MVC everywhere shows that conventions are better than configurations ).

In this case, add a controller! Add Example 6.

Figure 6. Figure 7.

Add a homecontroller to the controlles folder, for example, 7.

Write code in homecontroller!

View code

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace Hello_Asp.Net_Mvc.Controllers{    public class HomeController : Controller    {        public string Index()        {            return "Hello World";        }    }}

If you have something this time, run it. Result 8.

Figure 8.

But in MVC, we need to have a good understanding of routing: the MVC program also uses ASP. net routing to determine how the URL is mapped to the actual controller and action.

After the MVC project is created, the default route is added.

Therefore, when you enter three methods, the default route will direct your request to the index action (method) in the homecontroller;

  • /
  • /Home
  • /Home/Index

We enter http: // locahost: xxxx/or http: // locahost: xxxx/home or http: // locahost: xxxx/home/index will get "Hello World "!

You can also define routing rules on global. asax. The index method above returns a conversion character. To respond to an HTML page from the browser, we need to create a view.

Modify the index method in homecontroller,

View code

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace Hello_Asp.Net_Mvc.Controllers{    public class HomeController : Controller    {        public ViewResult Index()        {            ViewData["Message"] = "Hello World!";            return this.View();        }    }}

Code explanation: viewresult indicates a class that is used to present a view using the iview instance returned by the iviewengine object.

Then I add a view, right-click the index (Action) method of the homecontroller we modified, and select Add attempt, as shown in figure 9.

Figure 9.

Then, the "10" option box appears. Click "add. 10.

Figure 10.

In this case, vs automatically creates the view folder under the views folder and the index. cshtml view engine file. For example, 11.

Figure 11.

Then complete the index. cshtml page and let him display "Hello World" in HTML format!

The index. cshtml code is as follows:

@{    ViewBag.Title = "Asp.Net Mvc Index";}

After writing the script, run the program and check result 12.

Figure 12.

Here "Hello world! "As I have already said, O (lead _ lead) O is a new starter!

Note: The view name is the same as the action name in the controller, therefore, the routing automatically presents the HTML of the view named index to the browser. The return type of the action is collectively referred to

Actionresult, which is specific to viewresult.

Haha! New users can try other return types to learn, learn, and understand! I am also a newbie. If I describe whether there are errors or mistakes, please give me more advice and learn from them!

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.