鍩轰簬MVC鐨勪釜浜哄崥瀹㈢郴缁熻璁′笌瀹炵幇姣曚笟璁捐 - 鐧惧害鏂囧簱 联系客服

发布时间 : 星期日 文章鍩轰簬MVC鐨勪釜浜哄崥瀹㈢郴缁熻璁′笌瀹炵幇姣曚笟璁捐 - 鐧惧害鏂囧簱更新完毕开始阅读21de8fabd1d233d4b14e852458fb770bf78a3b3c

贵阳学院毕业设计(论文)

提示区 图5.6 博客网站母版页结构图

第二节 系统实现

由于本系统是基于ASP.NET MVC模式设计的,每个模块功能都对应着MVC的三层,即:Model, Controller和View。分有前台功能和后台管理,这里我们将分别描述。

一、前台实现

(一)博客首页

1.View: 打开博客网站首页后,如图5.7所示:

图5.7 博客网站首页

2.Controller类:对应的Controller类相关代码如下: public ViewResult index(int page) {

if (page * pageSize > BlogEntryManager.Count(0, 1)) page = page - 1; if (page <= 0) page = 1;

18

贵阳学院毕业设计(论文)

ViewData[\] = page;

List model = new List();

model = BlogEntryManager.GetList((page - 1) * pageSize, pageSize);

return View(model); } (二)用户评论

网站的浏览者可以对博客文章发表自己的评论。 1.View: 用户评论界面如图5.8所示:

图5.8 用户评论界面

2.Controller类:对应的Controller类相关代码如下:

public ActionResult AddComment(int page, string commentAuthor, string commentText) {

var comment = new Comment {

blog_id = page,

author = commentAuthor, body = commentText,

datecreated = DateTime.Now }; try {

if (commentAuthor.Trim().Length == 0)

ViewData.ModelState.AddModelError(commentAuthor, \);

if (commentText.Trim().Length == 0)

19

贵阳学院毕业设计(论文)

ViewData.ModelState.AddModelError(commentText, \Text not specified.\);

if (!ViewData.ModelState.IsValid)

throw new InvalidOperationException();

CommentManager.Save(comment); return RedirectToAction(\); }

catch (InvalidOperationException ex) {

return View(BlogEntryManager.GetItem(page)); } } (三)用户注册

未注册用户可以通过博客网站的注册功能注册成为网站会员。 1.View:用户注册界面如图5.9所示:

图5.9 用户注册界面

2.Controller类:对应的Controller类相关代码如下:

public ActionResult Register(string userName, string email, string password, string confirmPassword)

{

ViewData[\] = MembershipService.MinPasswordLength;

if (ValidateRegistration(userName, email, password, confirmPassword)) {

MembershipCreateStatus createStatus = MembershipService.CreateUser(userName, password, email);

if (createStatus == MembershipCreateStatus.Success) {

FormsAuth.SignIn(userName, false

20

贵阳学院毕业设计(论文)

return RedirectToAction(\, \); } else {

ModelState.AddModelError(\, ErrorCodeToString(createStatus));

} }

return View(); } (四)用户登录

注册用户登录博客系统后可以登录博客进行管理。 1.View:用户登录模块界面如图5.10所示:

图5.10 用户登录界面

2.Controller类:对应的Controller类相关代码如下:

public ActionResult LogOn(string userName, string password, bool rememberMe, string returnUrl)

{

if (!ValidateLogOn(userName, password)) {

ViewData[\] = rememberMe; return View(); }

FormsAuth.SignIn(userName, rememberMe); if (!String.IsNullOrEmpty(returnUrl)) {

return Redirect(returnUrl); } else {

return RedirectToAction(\, \); } }

21