Im creating a web application. There are a default page where a list of questions. When user click the question that will redirect to the user to ViewQuestion that is in the Question folder. At the default.aspx page im using the datalist control to display question title . And there i am generating the url with id to the question . For this code is below .
protected void listQuestion_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
LinkButton lnkTitle = (LinkButton)e.Item.FindControl("lnkQuestion");
// lnkTitle.Style.Add("text-decoration", "none");
PostEntity Item = (PostEntity)e.Item.DataItem;
lnkTitle.PostBackUrl = GenerateURL(Item.Title, Item.Id);
}
}
public static string GenerateURL(string title, int Id)
{
string strTitle = title.Trim();
strTitle = strTitle.ToLower();
//strTitle = strTitle.Replace();
strTitle = strTitle.Replace(" ", "-");
strTitle = strTitle.Trim();
strTitle = strTitle.Trim('-');
strTitle = "~/Questions/ViewQuestion.aspx?QuestionID=" + Id.ToString().Trim() + "/" + strTitle + ".aspx";
return strTitle;
}
in global.asax the code is
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RegisterRoute(RouteTable.Routes);
}
static void RegisterRoute(RouteCollection route)
{
route.MapPageRoute("Default", "Default", "~/Default.aspx");
route.MapPageRoute("ViewQuestion", "Questions/ViewQuestion{QuestionID}", "~/Questions/ViewQuestion.aspx");
}
And the viewpage to get the Querystring as below :
lblQustionText.Text = this.Page.RouteData.Values["QuestionID"].ToString() as string; // giving me object reference exception
my pageurl is generating like this
/Questions/ViewQuestion.aspx?QuestionID=1376/get-the-current-logged.aspx
How can i make this example for SEO friendly url . Thanks for your answer.