这次为BlogEngine的分类增加了自定义Url别名功能:
演示效果见本站:http://www.moozi.net/
修改教程:木子原创:http://www.moozi.net
http://www.moozi.net/archive/2008/08/06/BlogEngine-category-url-slug.aspx有疑问请留言,谢谢
修改代码:
BlogEngine.Core.Category.cs
在56行
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 description,string slug)
...{
this.Id = Guid.NewGuid();
this._Title = title;
this._Description = description;
this._Slug = slug;
this.Parent = null;
}
修改BlogEngine.Core.Providers.Categories.cs的
public override void InsertCategory(Category category),
public override void UpdateCategory(Category category),
public override void DeleteCategory(Category category)这些方法中作相应的修改
foreach (Category cat in categories)
...{
writer.WriteStartElement("category");
writer.WriteAttributeString("id", cat.Id.ToString());
writer.WriteAttributeString("description", cat.Description);
writer.WriteAttributeString("parent", cat.Parent.ToString());
writer.WriteAttributeString("slug", cat.Slug.ToString());//新增加的Url别名
writer.WriteValue(cat.Title);
writer.WriteEndElement();
cat.MarkOld();
}
修改public override List<Category> FillCategories()方法:
在
category.Id = new Guid(node.Attributes["id"].InnerText);
category.Title = node.InnerText;
后添加
if (node.Attributes["slug"] != null)
category.Slug = node.Attributes["slug"].InnerText;
else
category.Slug = string.Empty;
修改BlogEngine.Core.Web.HttpModules.UrlRewrite.cs中的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 Category.Categories)
...{
//string legalTitle = Utils.RemoveIllegalCharacters(cat.Title).ToLowerInvariant();
string legalTitle = Utils.RemoveIllegalCharacters(cat.Slug).ToLowerInvariant();
if (title.Equals(legalTitle, StringComparison.OrdinalIgnoreCase))
...{
context.RewritePath(Utils.RelativeWebRoot + "default.aspx?id=" + cat.Id.ToString() + GetQueryString(context), false);
break;
}
}
}
修改BlogEngine.Web/App_Code/Controls/CategoryList.cs中private HtmlGenericControl BindCategories()这个方法
129行开始:
HtmlAnchor anc = new HtmlAnchor();
//anc.HRef = Utils.RelativeWebRoot + "category/" + Utils.RemoveIllegalCharacters(key) + BlogSettings.Instance.FileExtension;
anc.HRef = Utils.RelativeWebRoot + "category/" + Utils.RemoveIllegalCharacters(GetSlug(new Guid(dic[key].ToString()))) + BlogSettings.Instance.FileExtension;
anc.InnerHtml = HttpUtility.HtmlEncode(key) + postCount;
anc.Title = "Category: " + key;
修改BlogEngine.Web/admin/pages/Categories.aspx
在"<asp:TextBox runat="Server" ID="txtNewCategory" Width="200" /><br />"后添加:
<asp:Label ID="lblNewSlug" runat="server" AssociatedControlID="txtNewSlug" Text="Slug" /><br />
<asp:TextBox runat="Server" ID="txtNewSlug" Width="200" /><br />
在"
<asp:TemplateField HeaderText="<%$ Resources:labels, name %>">
<ItemTemplate>
<%# Server.HtmlEncode(Eval("title").ToString()) %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtTitle" Text='<%# Eval("title") %>' />
</EditItemTemplate>
</asp:TemplateField>
"后添加:
<asp:TemplateField HeaderText="Slug">
<ItemTemplate>
<%# Server.HtmlEncode(Eval("slug").ToString())%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtSlug" Text='<%# Eval("slug") %>' />
</EditItemTemplate>
</asp:TemplateField>
修改BlogEngine.Web/admin/pages/Categories.aspx.cs中 void btnAdd_Click(object sender, EventArgs e)这个方法
将Category cat = new Category(txtNewCategory.Text, description);改成
string slug = txtNewSlug.Text;
if (slug.Length > 255)
slug = slug.Substring(0, 255);
Category cat = new Category(txtNewCategory.Text, description, slug);
修改 void grid_RowUpdating(object sender, GridViewUpdateEventArgs e)方法:
TextBox textboxTitle = (TextBox)grid.Rows[e.RowIndex].FindControl( " txtTitle " );
TextBox textboxSlug = (TextBox)grid.Rows[e.RowIndex].FindControl( " txtSlug " ); // 新增加的
TextBox textboxDescription = (TextBox)grid.Rows[e.RowIndex].FindControl( " txtDescription " );
DropDownList ddlParent = (DropDownList)grid.Rows[e.RowIndex].FindControl( " ddlParent " );
Category cat = Category.GetCategory(id);
cat.Title = textboxTitle.Text;
cat.Slug = textboxSlug.Text; // 新增加的
cat.Description = textboxDescription.Text;
if (ddlParent.SelectedValue == " 0 " )
cat.Parent = null ;
else
cat.Parent = new Guid(ddlParent.SelectedValue);
cat.Save();
Response.Redirect(Request.RawUrl);
到这里就修改完成了,改的东西有点多,比较烦,因为我打算对BlogEngine进行比较多的修改,所以暂时不提供修改的文件下载,等感觉改得差不多了再提供下载