FYJ.Blogs开发系列(四)-后台文章Controller
FYJ.Blogs开发系列(五)-前台基页Controller
FYJ.Blogs开发系列(六)-前台主页Controller
FYJ.Blogs开发系列(七)-前台文章Controller
using Blogs.UI.Main.Models; using FYJ.Web.NetPager; using Microsoft.Practices.Unity; using System; using System.Data; using System.IO; using System.Linq; using System.Text.RegularExpressions; using System.Web.Mvc; namespace Blogs.UI.Main.Controllers { public class BlogController : BaseController { [CompressAttribute] public ActionResult Index() { string viewName = "~/Views/Blog/Themes/" + base.ThemeName + "/Index.cshtml"; ActionResult result = Blogs.Common.ControllerHelper.CreateStaticHtml(this, viewName, () => { return GetBlogPageModel(); }, Server.MapPath("~/App_Data/html"), "index.html"); if (result == null) { return View(viewName, GetBlogPageModel()); } else { return result; } } private BlogPageModel GetBlogPageModel() { Models.BlogPageModel model = this.GetModel(); int recordCount = 0; //当前页 int page = Convert.ToInt32(RouteData.Values["page"]); if (page == 0) { page = 1; } int pageSize = 10; BindArticle(model, page, pageSize, out recordCount); model.PagerHtml = ActionPage(page, pageSize, recordCount); model.ThemeName = ThemeName; return model; } private string ActionPage(int page, int pageSize, int recordCount) { string baseUrl = this.Info.BaseUrl; string urlRewritePattern = baseUrl.TrimEnd('/') + "/page-{0}.html"; if (RouteData.Values["categoryid"] != null) { urlRewritePattern = baseUrl.TrimEnd('/') + "/cate-" + RouteData.Values["categoryid"] + "-{0}.html"; } if (RouteData.Values["tagid"] != null) { urlRewritePattern = baseUrl.TrimEnd('/') + "/tag-" + RouteData.Values["tagid"] + "-{0}.html"; } if (RouteData.Values["month"] != null) { urlRewritePattern = baseUrl.TrimEnd('/') + "/month-" + RouteData.Values["month"] + "-{0}.html"; } Pager pager = new Pager() { RecordCount = recordCount, PageSize = pageSize, MaxShowPageSize = 10, PageIndex = page, ShowSpanText = true, EnableUrlRewriting = true, UrlRewritePattern = urlRewritePattern, ClassName = "pagination", CurrentPageButtonCss = "current" }; string str = pager.ToString().Replace("page-1.html", ""); return str; } private void BindArticle(Models.BlogPageModel model, int page, int pageSize, out int recordCount) { ////tag categoryID 可以传''不能传null 因为存储过程Null判断有点问题 string categoryID = this.Info.categoryID ?? ""; if (RouteData.Values["categoryid"] != null) { categoryID = RouteData.Values["categoryid"].ToString(); } string tagID = (RouteData.Values["tagid"] == null) ? "" : RouteData.Values["tagid"].ToString(); int year = (RouteData.Values["month"] == null) ? 0 : Int32.Parse(RouteData.Values["month"].ToString().Substring(0, 4)); int month = (RouteData.Values["month"] == null) ? 0 : Int32.Parse(RouteData.Values["month"].ToString().Substring(4)); DataSet ds = ArticleDal.GetArticleDataSet(this.BlogID, page, pageSize, categoryID, tagID, year, month); model.TopArticles = this.DataTableToArticle(ds.Tables[0]); model.IndexArticles = this.DataTableToArticle(ds.Tables[1]); model.Title = ds.Tables[3].Rows[0]["title"].ToString(); recordCount = Convert.ToInt32(ds.Tables[2].Rows[0][0]); } } }
这里不同的分类和标签分页等只是主页显示的内容不同,所以关键只有2个页面,还有一个文章详细页