Fixed the link problem in BlogEngine. Net with full Chinese as the title (this problem has been fixed in Versions later than 1.1)

Source: Internet
Author: User
Tags blogengine
PS: This article only applies BlogEngine. Net1.0 valid !!! This problem does not exist in Versions later than 1.1. You can Download the latest version: Download
In BlogEngine. Net1.0The Post link is based on Post. when obtaining the relative Uri attribute RelativeLink of Post, the key statement is Utils. removeIlegalCharacters (Title). Let's first look at the RemoveIlegalCharacters () method: // <summary>
/// Strips all illegal characters from the specified title.
/// </Summary>
Public static string RemoveIlegalCharacters (string text)
{
If (string. IsNullOrEmpty (text ))
Return text;

StringBuilder sb = new StringBuilder ();
Text = text. Replace ("","-");
Foreach (char c in text)
{
If (IsAllowedCharacter (c ))
Sb. Append (c );
}

Return sb. ToString ();
}

Private static bool IsAllowedCharacter (char character)
{
String allowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 _-";
Foreach (char c in allowedChars)
{
If (c = character)
Return true;
}

Return false;
}

As you can see from the code above, if it is not "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 _-", this character will be ignored. For example, we enter the Chinese Abc for Utils. after RemoveIlegalCharacters ("Chinese Abc"), we will only get "Abc", so the link we see will be "/post/Abc. aspx ".
If the title contains the above characters, it is okay. If it is all composed of not the above characters, such as Utils. removeIlegalCharacters ("Chinese title"), the link displayed will be "/post /. aspx ", when there are two full-Chinese titles of Post, the previous Post will not be linked.

To solve this problem, I have made some modifications. The idea is as follows:
1. When the title contains characters such as "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 _-", it should be in the original format;
2. When the title is composed of a non "" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 _-"character, the Post. Id is used for the link, in the form ".

Step 1: Open the BlogEngine. Net source code Post. cs and modify the implementation of the RelativeLink attribute: // <summary>
/// A relative-to-the-site-root path to the post.
/// Only for in-site use.
/// </Summary>
Public Uri RelativeLink
{
// Get {return new Uri (VirtualPathUtility. ToAbsolute ("~ /Post/"+ Utils. RemoveIlegalCharacters (Title) +". aspx "), UriKind. Relative );}

// When all characters in the title are non-ASCII characters, the URL format is:/post/a9d8c39d-c731-43bc-9ac6-faeafcfe0fcf.aspx
// Where the a9d8c39d-c731-43bc-9ac6-faeafcfe0fcf is the string form of the Post Id
// Improved. Non-ASCII character title -- Start is supported.
Get
{
String link = Utils. RemoveIlegalCharacters (Title );
If (link. Length = 0)
{
Return new Uri (VirtualPathUtility. ToAbsolute ("~ /Post/"+ Id. ToString () +". aspx "), UriKind. Relative );
}
Return new Uri (VirtualPathUtility. ToAbsolute ("~ /Post/"+ link +". aspx "), UriKind. Relative );
}
// Improvement. supports non-ASCII character title -- End
}

Step 2: Open UrlRewrite. cs and modify the RewritePost () method: private static void RewritePost (HttpContext context)
{
// String title = ExtractTitle (context, "/post /");
// Post post = Post. GetPostByName (title );
// If (post! = Null)
// Context. RewritePath ("~ /Post. aspx? Id = "+ post. Id. ToString () + GetQueryString (context), false );

// When all characters in the title are non-ASCII characters, the URL format is:/post/a9d8c39d-c731-43bc-9ac6-faeafcfe0fcf.aspx
// Where the a9d8c39d-c731-43bc-9ac6-faeafcfe0fcf is the string form of the Post Id
// Improved. Non-ASCII character title -- Start is supported.
String title = ExtractTitle (context, "/post /");
Post post = Post. GetPostByName (title );
If (post! = Null)
{
Context. RewritePath ("~ /Post. aspx? Id = "+ post. Id. ToString () + GetQueryString (context), false );
Return;
}
Post = Post. GetPost (new Guid (title ));
If (post! = Null)
{
Context. RewritePath ("~ /Post. aspx? Id = "+ post. Id. ToString () + GetQueryString (context), false );
}
// Improvement. supports non-ASCII character title -- End
}

Step 3: recompile, run, and test:

Successful !!!

Similarly, you can modify the Category, Tag, and so on ....

I hope this article will be useful to you.

PS: The Chinese link of Title and Category has been fixed in Version 1.1, and the Chinese link of Tag has been fixed as follows:
Replace the RenderControl () method in App_Code/Controls/TagCloud. cs with the following method.
/// <Summary>
/// Renders the control.
/// </Summary>
Public override void RenderControl (HtmlTextWriter writer)
{
Writer. Write ("<ul id = \" tagcloud \ "> ");
Foreach (string key in WeightedList. Keys)
{
Writer. Write ("<li> ");
Writer. Write (string. Format (LINK, Utils. RelativeWebRoot + "? Tag =/"+HttpUtility. UrlEncode (key), WeightedList [key], "Tag:" + key, key ));
Writer. Write ("</li> ");
}

Writer. Write ("</ul> ");
Writer. Write (Environment. NewLine );
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.