日期:2014-05-16  浏览次数:20938 次

ASP.NET C# MVC 初学(一)

 

ASP.NET  C#  MVC 初学(一)

 

 

环境:VS 2010,MVC 3,不对之处,敬请指正。

 

一、初识MVC3

.

操作下如图,按确定按钮。


图1

 

在出现的下图中选择Internet应用程序,勾选创建单元测试项目,按确定。


图2

 

运行项目,出现以下页面。


 

图3

单击“关于”菜单项,出现如下页面。


 图4

单击“[登录]”项,出现如下页面。



图5

 

在解决方案资源管理器中,依次双击Controllers、HomeControler.cs项,显示HomeControler.cs的内容如下,可见Controler类有两个返回为AactionResult 类的方法Index和 About。这两个方法都调用了View()方法。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

 

namespace MvcApplication1.Controllers

{

    public class HomeController : Controller

    {

        public ActionResult Index()

        {

            ViewBag.Message = "欢迎使用 ASP.NET MVC!";

 

            return View();

        }

 

        public ActionResult About()

        {

            return View();

       

    }

}

 

在解决方案资源管理器中依次双击View、Home,可见Home下有两个cshtml文件,分别名为About和Index,如下图所示


图6

 

注意到Views下的Home即对应着解决方案资源管理器中Controllers中的HomeController.cs,其中的About.cshtml和Index.cshtml分别对应着HomeController.cs文件中的和 About和Index方法。

在解决方案资源管理器中双击About.cshtml或Index.cshtml,可以看到他们的内容,以下是About.cshtmlt和Index.cshtml的内容:

@{

    ViewBag.Title = "关于我们";

}