做为一名小小的GreenBird,学习MVC呢,已经花费了2天了,期间获得了美丽的学姐的帮助,初步整理了一下。html
首先,学习MVC呢就先以一个标准的MVC的简单的例子来入手,而后根据学姐的PPT,我用vs2012创建了一个项目。数据库
1.创建一个MVC架构的项目:浏览器
File->New Project->Visual C#->ASP.NET MVC 4 Web Application架构
能够直接运行这个网站mvc
2.Adding a Controllerasp.net
右键单击Controller->add->Controller(CommentControler)函数
public string Index()学习
{网站
return "This is my first test of <b>MVC</b>...";url
}
把原来的Index方法能够注释掉。
3.在RouteConfig.cs里面有路由规则。
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
能够试验一下这个路由规则:
在新建的CommentControler里新定义一个action以下:
public string Welcome(string name, int numTimes = 1)
{
return HttpUtility.HtmlEncode("Hello " + name + ", NumTimes is: " + numTimes);
}
而后在浏览器上面
也能够改变一下路由规则:
routes.MapRoute(
name: "Cathy",
url: "{controller}/{action}/{name}/{id}"
);
改变一下Comment里面的action:
public string Welcome(string name, int ID = 1)
{
return HttpUtility.HtmlEncode("Hello " + name + ", ID: " + ID);
}
4.Add a folder View|Comment
引用:Layout = "~/Views/Shared/_Layout.cshtml";(能够在Share里面的要引用的东西直接拖过来稍微修改一下就能够了)
Index里面的内容是这个样子的:
@{
ViewBag.Title = "Easy";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Practice of *Comments</h2>
5.修改CommentController里面的内容,
public ActionResult Index()
{
return View();
}
把此注释去掉。
6.在Share的公共模板里面修改界面的格式,menu里面增长一个超连接的文字。
(能够输入函数名称,把鼠标放在上面看下面的参数应该怎么写)
<li>@Html.ActionLink("Easy", "Index", "Comment")</li>
7.Adding a Model:Comment.cs
using System;
using System.Data.Entity;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Practice.Models
{
public class Comment
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int CommentID { get; set; }
public DateTime CommentTime { get; set; }
public string CommnetUser { get; set; }
public string CommenContent { get; set; }
public int ParentCommentID { get; set; }
}
}
8.在Models 里面的另外的cs文件中寻找Dbcontext,并写入:
public DbSet<Comment> Comments { get; set; }
9.在菜单栏里面找到tools->libraries package manager->package manager console
分别输入下面的命令:
Enable-Migrations//会在工程里面自动生成一个Migrations文件
Add-Migration Create_DB_DIY(本身定义的,可是没有间隔)
Update-Database
这样就在vs里面建立了一个数据库,里面有两张表,能够在View->Server Explore里面找到tables,有两张表:
能够右击表,New Query,新建查询
10.设计Easy超连接的页面Index.cshtml
<div>
<div>
<table>
<tr>
<td>
<div>
<div style="color: blue">CommnetUser</div>
<div>CommenContent</div>
<div style="color: gray; font-size: 10px;">CommentTime</div>
</div>
</td>
<td>
<div>
<input id="delete-button" type="submit" value="Delete" />
</div>
</td>
</tr>
</table>
</div>
<div>
<div>
<input name="commentUser" type="text" placeholder="User Name..." />
</div>
<div>
<textarea name="commentContent" rows="5"></textarea>
</div>
<div>
<input id="submit-button" type="submit" value="Submit" />
</div>
</div>
</div>
到了这个地方,咱们就能够获得显示的页面了,可是这个时候还不能与数据库链接,须要新建Controller来控制与数据库的交互。
11.新建Controller:BaseController.cs增长内容:
public UsersContext db = new UsersContext();
另外为了和数据库进行交互,还需包含下面的using System.Web.Mvc;
using MVC4.Models;
12.修改CommentController.cs
using CommentSample.Models;
Change Controller into BaseController
public ActionResult Index()
{
var comments = db.Comments.OrderByDescending(c => c.CommentTime).ToList();
return View(comments);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CommentFunction(string commentUser, string commentContent)
{
if (commentUser != null && commentContent != null)
{
db.Comments.Add(new Comment { CommentTime = DateTime.Now, CommnetUser = commentUser, CommenContent = commentContent, ParentCommentID = 0 });
db.SaveChanges();
}
return RedirectToAction("Index", "Comment");
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult DeleteFunction(int commentId)
{
var comment = db.Comments.SingleOrDefault(c => c.CommentID == commentId);
if (comment != null)
{
db.Comments.Remove(comment);
db.SaveChanges();
}
return RedirectToAction("Index", "Comment");
}
}
13.修改Comment/Index.cshtml:
@model List<MVC4.Models.Comment>
@using MVC4.Models
@{
ViewBag.Title = "Easy";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div>
<div>
<table>
@foreach (var comment in Model.Where(c => c.ParentCommentID == 0))
{
<tr>
<td>
<div>
<div style="color: blue">@comment.CommnetUser</div>
<div>@comment.CommenContent</div>
<div style="color: gray; font-size: 10px;">@comment.CommentTime</div>
</div>
</td>
<td>
@using (Html.BeginForm("DeleteFunction", "Comment", "FormMethod.Post"))
{
@Html.AntiForgeryToken()
@Html.Hidden("commentId",comment.CommentID)
<div>
<input id="delete-button" type="submit" value="Delete" />
</div>
}
</td>
</tr>
}
</table>
</div>
@using (Html.BeginForm("CommentFunction", "Comment", FormMethod.Post))
{
@Html.AntiForgeryToken()//防止CSRS攻击
<div>
<div>
<input name="commentUser" type="text" placeholder="User Name..." />
</div>
<div>
<textarea name="commentContent" rows="5"></textarea>
</div>
<div>
<input id="submit-button" type="submit" value="Submit" />
</div>
</div>
}
</div>
这样的话就创建了一个简单的留言簿。能够输入评论人的名字,评论内容,提交后显示在页面上,页面上的每一个评论均可以删除。
期间出现了问题:
The resource cannot be found.
下面的网址提供了解决方案:
http://cephas.net/blog/2005/07/14/aspnet-the-resource-cannot-be-found/
可是这个对于我来讲好像并无什么用处,Fiona学姐给我解决了这个问题,就是我没有把CSRS攻防的方法放在正确的位置上。
每一个增删数据的方法对应于Controller.cs里面的一个方法,该方法上面要加上相应的防止CSRS攻防的语句。同时,在View里利用@来定义一个Action里所对应的模型,这样就能够直接在cshtml文件中调用“Model”变量。 Model实际上是一个类库,封装与应用程序的业务逻辑相关的数据以及对数据的处理方法,它能够直接访问数据库。对于一个事件,Controller首先会根据这个事件做出相应的处理,去改变View或Model。
原文地址:(http://blog.csdn.net/qinkeliangqin/article/details/27084639)
1. 当第一个请求从客户端发起的时候,首先执行的是Global.asax中的Application_Start()方法来完成一些初始化工做,其中重要的一步是RegisterRoutes方法,这个方法指定了如何将url映射到具体的方法上,稍后详解。
2. 根据第一步中指定的映射表生成一个RouteData对象,利用这个对象来建立一个RequestContext对象。
3. MvcRouteHandler建立一个MvcHandler,并将RequestContext对象传给MvcHandler。
4. MvcHandler对象利用RequestContext对象肯定一个IControllerFactory对象来建立Controller对象。
5. MvcHandler对象调用Controller对象的Execute()方法。
6. Controller的ControolerActionInvoker对象决定调用controller的哪一个具体的action方法。
7. Action方法接受用户参数,执行方法,返回一个Result类型的对象。
注:其实还有不少问题没有搞清楚,之后接触的过程当中会继续学习~~
参考文献:
http://www.cnblogs.com/zgqys1980/archive/2012/08/17/2643572.html