這次為BlogEngine的分類增加了自定義Url別名功能
修改教程:木子原創
category
url
slug
aspx有疑問請留言
謝謝
修改代碼
BlogEngine
Core
Category
cs
在
行
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()這個方法
行開始
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=
/><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>
<%# 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 >
)
slug = slug
Substring(
);
Category cat = new Category(txtNewCategory
Text
description
slug);
修改 void grid_RowUpdating(object sender
GridViewUpdateEventArgs e)方法
Guid id = (Guid)grid
DataKeys[e
RowIndex]
Value;
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 ==
)
cat
Parent = null;
else
cat
Parent = new Guid(ddlParent
SelectedValue);
cat
Save();
Response
Redirect(Request
RawUrl);
到這裡就修改完成了
改的東西有點多
比較煩
因為我打算對BlogEngine進行比較多的修改
所以暫時不提供修改的文件下載
等感覺改得差不多了再提供下載
From:http://tw.wingwit.com/Article/program/net/201311/13495.html