[翻译:ASP.NET MVC 教程]创建一个控制器

本教程的目标是向你解释怎样创建新的ASP.NET MVC控制器。你会了解到怎样创建控制器的过程,通过使用Visual Studio Add Controller菜单选项以及手工创建一个新类。


使用Add Controller菜单选项

创建控制器最简单的方法是右击Visual Studio解决方案浏览窗口中的Controllers文件夹,然后选择Add, Controller菜单选项(见图1)。选择该菜单选项并打开Add Controller对话框(见图2)。

clip_image002

图1:添加新控制器

clip_image004

图2:Add Controller对话框

注意到在Add Controller对话框中的控制器名称的第一部分被高亮显示。每一个控制器名称必须以后缀Controller结尾。例如,你可以创建一个名为ProductController的控制器,但不能是一个名为Product的控制器。

clip_image005

清单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中的控制器类将被生成。

clip_image007

图3:自动创建动作方法

清单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)
        {
            try
            {
                // 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)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}

这些被生成的方法仅仅是初步的方法。你自己必须为creating,updating以及showing details添加实际的操作逻辑。但是,这些初步的方法为你提供了一个好的开始。


创建控制器类

ASP.NET MVC控制器仅仅只是一个类。如果你愿意,你可以不使用便捷的Visual Studio控制器搭建程序,而是通过手工创建一个控制器类。按下列步骤操作:

1. 右击Controllers文件夹,然后选择菜单选项Add, New Item,再选择Class模板(见图四)。

2. 命名新建类为PersonController.cs,然后点击Add按钮。

3. 修改结果类文件,使得该类继承自基类System.Web.Mvc.Controller类(见清单3)。

clip_image009

图4:创建一个新类

清单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

clip_image010

作者:Kinglee
文章出处:Kinglee’s Blog ( http://www.cnblogs.com/Kinglee/)
版权声明:本文的版权归作者与博客园共有。转载时须注明本文的详细链接,否则作者将保留追究其法律责任。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值