Let’s suppose you want to use a separate home page for your BlogEngine.NET website. Now let’s suppose you want to display the most recently published post on that page. Up till now there wasn’t an easy way to do this without inheriting the BlogEngine Base Page and thus being under the site.master page reference. Well now there is an easy way.
Today I give you an ASP.NET User Control that displays the most recently published blog post on any page you wish. To help make this User Control very flexible I have added a couple of options:
| ShowExcerpt = "false" | Display the entire post or only the description/subset of the post |
| BodyLength = "300" | While ShowExcerpt is true – limit body length to this length |
| ShowBody = "true" | Display the body of the post? |
| ShowAuthor = "true" | Display the author of the post? |
| ShowDate = "true" | Display the date of the post? |
| ShowTitle = "true" | Display the title of the post? |
| postID = "{GUID}” | Specify a post GUID and it will be displayed |
To add this to a stand-alone page you need to first copy the User Control file into your ~/User Controls folder and then add a reference to the User Control:
<%@ Register Src="~/User controls/SinglePost.ascx" TagName="SinglePost" TagPrefix="sp" %>
Next you need to add the User Control tag wherever you want the post to be displayed:
<sp:SinglePost ID="singlepost1" runat="server"
ShowExcerpt="false"
ShowBody="true"
ShowAuthor="true"
ShowDate="true"
ShowTitle="true"
BodyLength="300" />
You also have the option of adding a specific GUID that you want to display. For example, if you add the option postID=”” then that specific post would be displayed instead of the most recently published post.
<sp:SinglePost ID="singlepost1" runat="server"
postID="4e02a8a7-7d7e-4f63-946c-79ac86a5a0e3" />
You can find a post ID by looking at the permalink for any post. Pretty cool, huh?!
Anyway, the HTML results will look like the following:
<div id="SinglePost">
<h2 id="spTitle">
<a rel="me" href="http://my.domain.com/post/2008/10/09/Your-Test-Post-Here.aspx">Your Test Post Here</a>
</h2>
<span id="spDate">10/9/2008 12:34:32 AM</span>
<span id="spAuthor">Posted by: YourNameHere</span>
<div id="spContent">
...
</div>
</div>
You will definitely want to apply some CSS magic to the results to get that special look. And don’t forget, you can use the User Control options to remove the items like date and author instead of using CSS! And that’s it! Feel free to give it a test drive and let me know if you have any problems or questions!
SinglePost-UserControl.zip