在本教程中,Stephen Walther 演示如何将控制器添加到 ASP.NET MVC 应用程序。

本教程的目的是说明如何创建新的 ASP.NET MVC 控制器。 了解如何使用 Visual Studio 的“添加控制器”菜单选项和手动创建类文件来创建控制器。

使用“添加控制器”菜单选项

创建新控制器的最简单方法是右键单击 Visual Studio 解决方案资源管理器窗口中的“控制器”文件夹,然后选择“ 添加”、“控制器 ”菜单选项 (请参阅图 1) 。 选择此菜单选项将打开 “添加控制器 ”对话框, (请参阅图 2) 。

图 01 :添加新控制器 ( 单击以查看全尺寸图像 )

图 02 :“添加控制器”对话框 ( 单击以查看全尺寸图像 )

请注意,控制器名称的第一部分在 “添加控制器 ”对话框中突出显示。 每个控制器名称都必须以后缀 控制器 结尾。 例如,可以创建名为 ProductController 的控制器, 但不能创建名为 Product 的 控制器。

如果创建的控制器缺少 控制器 后缀,则无法调用该控制器。 不要这样做 -- 犯这个错误后, 我浪费了我一生中无数小时。

列表 1 - Controllers\ProductController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
namespace MvcApplication1.Controllers
    public class ProductController : Controller
        // GET: /Product/
        public ActionResult Index()
            return View();

应始终在 Controllers 文件夹中创建控制器。 否则,将违反 ASP.NET MVC 的约定,其他开发人员将更难理解应用程序。

基架操作方法

创建控制器时,可以选择自动生成 Create、Update 和 Details 操作方法 (请参阅图 3) 。 如果选择此选项,则会生成清单 2 中的控制器类。

图 03:自动创建操作方法 (单击以查看全尺寸图像)

清单 2 - Controllers\CustomerController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
namespace MvcApplication1.Controllers
    public class CustomerController : Controller
        // GET: /Customer/
        public ActionResult Index()
            return View();
        // GET: /Customer/Details/5
        public ActionResult Details(int id)
            return View();
        // GET: /Customer/Create
        public ActionResult Create()
            return View();
        // POST: /Customer/Create
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection collection)
                // TODO: Add insert logic here
                return RedirectToAction("Index");
            catch
                return View();
        // GET: /Customer/Edit/5
        public ActionResult Edit(int id)
            return View();
        // POST: /Customer/Edit/5
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection collection)
                // TODO: Add update logic here
                return RedirectToAction("Index");
            catch
                return View();

这些生成的方法是存根方法。 必须添加实际逻辑,以便自行创建、更新和显示客户的详细信息。 但是,存根方法提供了一个很好的起点。

创建控制器类

ASP.NET MVC 控制器只是一个类。 如果愿意,可以忽略方便的 Visual Studio 控制器基架,手动创建控制器类。 按照以下步骤操作:

  • 右键单击“控制器”文件夹,然后选择菜单选项 “添加”、“新建项 ”,然后选择 “类 ”模板 (请参阅图 4) 。
  • 将新类命名为 PersonController.cs,然后单击“ 添加” 按钮。
  • 修改生成的类文件,以便类继承自基本 System.Web.Mvc.Controller 类 (请参阅列表 3) 。
  • 图 04:创建新类 (单击以查看全尺寸图像)

    列表 3 - Controllers\PersonController.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    namespace MvcApplication1.Controllers
        public class PersonController : System.Web.Mvc.Controller
            public string Index()
                return "Hello World!";
    

    清单 3 中的控制器公开一个名为 Index () 的操作,该操作返回字符串“Hello World!”。 可以通过运行应用程序并请求 URL 来调用此控制器操作,如下所示:

    http://localhost:40071/Person

    ASP.NET Development Server 使用随机端口号 (,例如 40071) 。 输入 URL 以调用控制器时,需要提供正确的端口号。 你可以通过将鼠标悬停在屏幕) windows 通知区域 (右下角 ASP.NET 开发服务器的图标上来确定端口号。

    上一页下一页