ASP. NET mvc3 + EF4 + Oracle entry-level instance (2)

Source: Internet
Author: User
Tags visual studio 2010

2.4 create your first application

Open Visual Studio 2010, select a file, select new project, select VisualC # on the left, and then selectASP. net mvc 3 Web Application. Name the project as "mvcmovie" and click "OK ".

In the new ASP. net mvc 3 Project window that appears, select Internet application. Make sure to use HTML5 markup and retain razor as the default view engine.

Click "OK". Visual Studio has used the default template to create an application that can be run for you. This is a simple "Hello world !" Program, it is a good start for you.

Open the Debug menu and select start debugging"

You can also use the shortcut key F5 to start debugging.

Visual Studio starts a browser and opens the main page of the program. Note that the address bar displays localhost instead of example.com. This is because localhost always points to your local computer. In this case, it points to the application you just created. When Visual Studio starts a network project, a random port is used by the Web server.

As shown in, this is a random number of 43246. When you run the program, you may see a different port.

2.5 Add a controller

MVC stands for Model-View-controller. MVC is a good architecture and maintenance mode for developing applications. MVC-based programs include:

Controller: processes external requests, obtains model data, and extracts view templates (returns requests to clients );

Model: Business Rules that represent application data and use verification logic to execute data;

View: Some template files used to dynamically generate HTML responses.

This tutorial will include all the above concepts and show you how to use them to create applications.

Let's start to create a control class. In Solution Explorer, right-click the Controller folder and choose add controller.

Name the new controller as "helloworldcontroller ". Retain the default templateEmpty Controller. ClickAdd.

Note: a new file in Solution Explorer has been created and is named helloworldcontroller. cs. The file has been opened in IDE.

Create two methods in the public class helloworldcontroller of the program block. The Code is as follows. As an example, the Controller returns an HTML string.

using System.Web;using System.Web.Mvc;namespace MvcMovie.Controllers{    public class HelloWorldController : Controller    {        //                 // GET: /HelloWorld/                  public string Index()        {            return "This is my <b>default</b> action...";        }        //                 // GET: /HelloWorld/Welcome/                 public string Welcome()        {             return "This is the Welcome action method...";         }    }}

Your controller is named helloworldcontroller and the first method is named index. Let's call it from a browser. Run the application (Press F5 or Ctrl + F5 ). Append "helloworld" to the address in the browser ". (For example, in the following illustration, write http: // localhost: 43246/helloworld .)

The following screen is returned on the browser page. In the above method, the Code directly puts back the string. You told the system to return only some HTML, and it did it!

The first part of the URL determines which controller class to execute. Therefore,/helloworld maps to the helloworldcontroller class.

The second part of the URL determines the operation (method) in the Controller class ). Therefore,/helloworld/index triggers the index method of the helloworldcontroller class.

Note: The index method is used as the default method (configured in the route ).

This is why the index method will be called if the method is not explicitly specified.

Browse ttp: // localhost: xxxx/helloworld/welcome. The welcome method returns the string "this is the welcome action method ...".

The default MVC ing is/[controller]/[actionname]/[parameters].

For this URL, the controller is helloworld and welcome is the response method. You do not need to use [parameters].

Let's modify it a bit so that you can pass parameters to the Controller through the URL. (For example,/helloworld/welcome? Name = Scott & numtimes = 4). Change the welcome method and add two parameters as follows. Note: Use the optional parameter feature of C # In the code to specify that the default value of numtimes is 1 (when this parameter is not passed ).

public string Welcome(string name, int numTimes = 1){    return HttpUtility.HtmlEncode("Hello " + name + ", NumTimes is: " + numTimes);}

Run the application and browse the URL (http: // localhost: xxxx/helloworld/welcome? Name = Scott & numtimes = 4 ).

You can try other values for name and numtimes multiple times. The system automatically reflects the parameters in your method from the URL request string.

In the two examples, the Controller has completed "V and C" in MVC, that is, the view and controller have been working. The Controller directly returns html. Generally, you do not want the Controller to directly return HTML because it turns into a very cumbersome code. We usually use a separate view template file to help generate HTML responses.

Next, let's take a look at how we can achieve this.

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.