熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

為BlogEngine的分類增加自定義Url別名功能

2022-06-13   來源: .NET編程 
這次為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進行比較多的修改所以暫時不提供修改的文件下載等感覺改得差不多了再提供下載
From:http://tw.wingwit.com/Article/program/net/201311/13495.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.