Text URL for website in asp.net
I am developing my blog website in asp.net. I am saving my blog post in sql server 2005 database.
I 开发者_StackOverflow社区am storing like this in my database
- PostId INT IDENTITY
- PostText
- PostAuthor
- PostedDate
- IsActive
So, at the code side when user is clicking on the tag/link. It is comming like this http://www.mysite.com/readBlog.aspx?Id=2
On the readBlog.aspx page I am reading the URL and fetching the data from the database. But this is something unprofessional. I want URL like this
Scott Gu post
How can I achieve it in simpler manner?
EDIT
It is just when I want to share the URL of my website I can do like this
http://www.mysite.com/readBlog/How-to-install-visual-studio
Take a look onto ASP.NET Routing
.
Just register a route in your Global.asax
:
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("readblog",
"readblog/{id}/{title}",
"~/readBlog.aspx");
}
and then it could be accessed by using e.g.:
var path = RouteTable.Routes.GetVirtualPath(
null,
"readblog",
new RouteValueDictionary {{ "id", blogId }, { "title", URLFriendly(title) }}
).VirtualPath;
where URLFriendly
produces URL-friendly version of a title (a very nice example of it could be taken from Jeff Atwood's answer). This could produce URLs like:
/readBlog/2/how-to-install-visual-studio
or
/readBlog/3/something-else
.
You will want to look into URL Rewriting.
Maybe take a look at a Scott Gu Post on the subject.
精彩评论