这次为BlogEngine的分类增加了自定义Url别名功能 修改教程:木子原创 categoryurlslugaspx有疑问请留言谢谢 修改代码 BlogEngineCoreCategorycs 在行 private string _Title; /**//// <summary> /// Gets or sets the Title or the object /// </summary> public string Title { get { return _Title; } set { if (_Title != value) MarkChanged(Title); _Title = value; } } 后添加 private string _Slug; /**//// <summary> /// Gets or sets the Slug or the object /// </summary> public string Slug { get { return _Slug; } set { if (_Slug != value) MarkChanged(Slug); _Slug = value; } } public Category(string title string descriptionstring slug) { thisId = GuidNewGuid(); this_Title = title; this_Description = description; this_Slug = slug; thisParent = null; } 修改BlogEngineCoreProvidersCategoriescs的 public override void InsertCategory(Category category) public override void UpdateCategory(Category category) public override void DeleteCategory(Category category)这些方法中作相应的修改 foreach (Category cat in categories) { writerWriteStartElement(category); writerWriteAttributeString(id catIdToString()); writerWriteAttributeString(description catDescription); writerWriteAttributeString(parent catParentToString()); writerWriteAttributeString(slug catSlugToString());//新增加的Url别名 writerWriteValue(catTitle); writerWriteEndElement(); catMarkOld(); } 修改public override List<Category> FillCategories()方法 在 categoryId = new Guid(nodeAttributes[id]InnerText); categoryTitle = nodeInnerText; 后添加 if (nodeAttributes[slug] != null) categorySlug = nodeAttributes[slug]InnerText; else categorySlug = stringEmpty; 修改BlogEngineCoreWebHttpModulesUrlRewritecs中的private static void RewriteCategory(HttpContext context string url) private static void RewriteCategory(HttpContext context string url) { string title = ExtractTitle(context url); foreach (Category cat in CategoryCategories) { //string legalTitle = UtilsRemoveIllegalCharacters(catTitle)ToLowerInvariant(); string legalTitle = UtilsRemoveIllegalCharacters(catSlug)ToLowerInvariant(); if (titleEquals(legalTitle StringComparisonOrdinalIgnoreCase)) { contextRewritePath(UtilsRelativeWebRoot + defaultaspx?id= + catIdToString() + GetQueryString(context) false); break; } } } 修改BlogEngineWeb/App_Code/Controls/CategoryListcs中private HtmlGenericControl BindCategories()这个方法 行开始 HtmlAnchor anc = new HtmlAnchor(); //ancHRef = UtilsRelativeWebRoot + category/ + UtilsRemoveIllegalCharacters(key) + BlogSettingsInstanceFileExtension; ancHRef = UtilsRelativeWebRoot + category/ + UtilsRemoveIllegalCharacters(GetSlug(new Guid(dic[key]ToString()))) + BlogSettingsInstanceFileExtension; ancInnerHtml = HttpUtilityHtmlEncode(key) + postCount; ancTitle = Category: + key; 修改BlogEngineWeb/admin/pages/Categoriesaspx 在<asp:TextBox runat=Server ID=txtNewCategory Width= /><br />后添加 <asp:Label ID=lblNewSlug runat=server AssociatedControlID=txtNewSlug Text=Slug /><br /> <asp:TextBox runat=Server ID=txtNewSlug Width= /><br /> 在 <asp:TemplateField HeaderText=<%$ Resources:labels name %>> <ItemTemplate> <%# ServerHtmlEncode(Eval(title)ToString()) %> </ItemTemplate> <EditItemTemplate> <asp:TextBox runat=server ID=txtTitle Text=<%# Eval(title) %> /> </EditItemTemplate> </asp:TemplateField> 后添加 <asp:TemplateField HeaderText=Slug> <ItemTemplate> <%# ServerHtmlEncode(Eval(slug)ToString())%> </ItemTemplate> <EditItemTemplate> <asp:TextBox runat=server ID=txtSlug Text=<%# Eval(slug) %> /> </EditItemTemplate> </asp:TemplateField> 修改BlogEngineWeb/admin/pages/Categoriesaspxcs中 void btnAdd_Click(object sender EventArgs e)这个方法 将Category cat = new Category(txtNewCategoryText description);改成 string slug = txtNewSlugText; if (slugLength > ) slug = slugSubstring( ); Category cat = new Category(txtNewCategoryText description slug); 修改 void grid_RowUpdating(object sender GridViewUpdateEventArgs e)方法 Guid id = (Guid)gridDataKeys[eRowIndex]Value; TextBox textboxTitle = (TextBox)gridRows[eRowIndex]FindControl(txtTitle); TextBox textboxSlug = (TextBox)gridRows[eRowIndex]FindControl(txtSlug);//新增加的 TextBox textboxDescription = (TextBox)gridRows[eRowIndex]FindControl(txtDescription); DropDownList ddlParent = (DropDownList)gridRows[eRowIndex]FindControl(ddlParent); Category cat = CategoryGetCategory(id); catTitle = textboxTitleText; catSlug = textboxSlugText;//新增加的 catDescription = textboxDescriptionText; if (ddlParentSelectedValue == ) catParent = null; else catParent = new Guid(ddlParentSelectedValue); catSave(); ResponseRedirect(RequestRawUrl); 到这里就修改完成了改的东西有点多比较烦因为我打算对BlogEngine进行比较多的修改所以暂时不提供修改的文件下载等感觉改得差不多了再提供下载 |