zoukankan      html  css  js  c++  java
  • 添加控制器 Adding a Controller

    模型-视图-控制器 是MVC的标准。MVC是一个架构良好、可测试和易于维护的程序开发模式。基于MVC的程序包含:

    • 模型:用于表示程序的数据和根据业务规则实施数据验证逻辑的类。
    • 视图:你的程序动态生成HTML响应内容的模板文件。
    • 控制器:用于捕获来自浏览器的请求、发现模型数据以及根据指定的视图模板文件响应浏览器的类。

    MVC stands for model-view-controller. MVC is a pattern for developing application that are well architedcted, testable and easy to maintain. MVC based applications contain:

    • Models: Classes that represent the data of the application and that use validation logic to enforce business rules for that data.
    • Views: Template files that your application uses to dynamically generate HTML responses.
    • Controllers: Classes that handle incoming browser requests, retrieve model data, and then specify view templates that return a response to the browser.

    我们会在本系列教程中全面涵盖这些概念并向你展示如何使用它们构建应用程序。

    We'll be covering all these concepts in this tutorial series and show you how to use them to build an application.

    让我们开始创建一个控制器类吧。在方案浏览器中,鼠标右击控制器文件夹然后选择添加控制器

    Let's begin by creating a controller class. In Solution Explorer, right-click Controllers folder and then select Add Controller

    为你的新控制器命名为 “HelloWorldController”。保留默认模板,比如:空白的MVC控制器,点击添加按钮。

    Name your new controller "HelloWorldController". Leave the default template as Empty MVC controller and click Add.

    add controller

    注意在方案浏览器中已经创建了一个名为 HelloWorldController.cs 的新文件,并且在IDE中已打开了。

    Notice in Solution Explorer that a new file has been created named HelloWorldController.cs. The file is open in the IDE.

    使用下面的代码替换文件中的内容。

    Replace the contents of the file with the following code.

    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..."; 
            } 
        } 
    }
    

      

    控制器方法将返回一个像示例那样的HTML字符串。控制器的名称是  HelloWorldController 并有了第一个名称 Index 的方法。让我们在浏览器中调用它们。运行程序(按F5键或Ctrl+F5)。在浏览器地址栏的路径后边加上 "HelloWorld"。(比如:像上面说的那样,地址就是 http://localhost:1234/HelloWorld)。在浏览中会看到和下面截图一样。在这个方法之前,代码直接返回字符串。你告诉系统只返回一些HTML,所以它就这样做了。

    The controller methods will return a string of HTML as an example. The controller is named HelloWorldController and the first method above is named Index. Let's invoke it from browser. Run the application (press F5 or Ctrl+F5). In the browser, append "HelloWorld" to the path in the address bar. (For example, in the illustration below,it's http://localhost:1234/HelloWorld.) The page in the browser will look like the following screenshot. In the method above, the code returned a string directly. You told the system to just retutn some HTML,and it did!

    ASP.NET MVC 依据请求的URL调用不同控制器类(不同的 action 方法也是如此)。ASP.NET MVC 使用像下面这样格式的默认URL路由逻辑代码来决定它将如何调用。

    ASP.NET MVC invokes different controller classes (and different action methods within them) depending on th incoming URL. The default URL routing logic used by ASP.NET MVC uses a format like this to determine what code to invoke:

    /[Controller]/[ActionName]/[Parameters]

    URL的第一部分决定要执行的控制器类。因此  /HelloWorld 映射到 HelloWorldController  类。URL的第部分决定调用控制器类中的 action方法。因此 /HelloWorld/Index 执行的是HelloWorldController类中的Index方法。注意我们在浏览器只输入了 /HelloWorld并且默认使用了 Index方法,那是因为  Index 方法是默认方法,在调用的时候不必每次都明确的定义。

    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 would cause the Index method of the HelloWorldController class to execute. Notice that we only had to browser to /HelloWorld and the Index method was used by default. This is because a method named Index is default method that will be called on a controller if one is not explicitly specified.

    浏览地址  http://localhost:xxxx/HelloWorld/Welcome。Welcome 方法运行并返回字符串 “This is the Welcome action method...”。 默认MVC映射的是 。像这个URL,控制就是HelloWorld ,action方法就是Welcome。你还有一个未使用的[Parameters]URL部分

    Browse to http://localhost:xxxx/HelloWorld/Welcome. The Welcome method runs and returns the string "This is the Welcome action method...". The default MVC mapping is /[Controller]/[ActionName]/[Parameters]. For this URL, the controller is HelloWorld and Welcome is the action method. You haven't used the [Parameters] part of the URL yet.

    让我们轻微的改下这个例子,使你能通过URL传些参数信息给控制器(例如,/HelloWorld/Welcome?name=Scott&numtimes=4)。更改 Welcome方法让这包含两参数并显示它们。注意:这个代码使用了C#的可选参数功能,表明 numTimes 参数在没有给它传参时使用1做为默认值。

    Let's modify the example slightly so that you can pass some parameter information from the URL to the controller (for example, /HelloWorld/Welcome?name=Scott&numtimes=4). Change your Welcome method to include two parameters as shown below. Note that the code uses the C# optional-parameter feature to indicate that thenumTimes parameter should default to 1 if no value is passed for that parameter.

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

      

    运行你的程序,浏览像这样的URL (http://localhost:xxxx/HelloWorld/Welcome?name=Scott&numtimes=4),你可以尝试给参数 name 和numtimes使用的不同的值。ASP.NET 模型绑定系统 从地址栏的查询字符串自动映射命名参数到给你的方法。

    Run your application and browse to the example URL (http://localhost:xxxx/HelloWorld/Welcome?name=Scott&numtimes=4). You can try different values for name and numtimes in the URL. The ASP.NET MVC model binding system automatically maps the named parameters from the query string in the address bar to parameters in your method.

    在这两个例子中使用了MVC的VC两部分,展示了视图与控制器的工作,控制器直接返回HTML。通常你不希望直接返回HTML,那样一来代码会变得非常冗长。替代我们原来的方法就是采用分享的视图模板文件来帮助生成HTML响应。在接下来我们将知道如何做到它。

    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 you 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.

  • 相关阅读:
    Java 数量为5的线程池同时运行5个窗口买票,每隔一秒钟卖一张票
    Android Notification
    Android DatePickerDialog TimePickerDialog
    Android Toast 提示按两次返回键退出
    Android Toast 自定义
    Android ProgressDialog 加载进度
    Android 自定义Dialog
    Android Dialog AlertDialog
    Android BaseAdapter ListView (明星简介列表)
    Android SimpleAdapter ListView (锁定手机,解锁手机的列表)
  • 原文地址:https://www.cnblogs.com/junhwong/p/2877494.html
Copyright © 2011-2022 走看看