0

I have a simple web form application. I have one page called "show.aspx" which is responsible for displaying my article by an article Id.

At first I used to use QueryString to fetch article Id and on the basis of that Id I displayed article. However I knew that the url was not SEO friendly.

My URL was like this: mysite.com/article/show.aspx?articleid=7737

So I decided to move to URL rewriting, but since I had no experience in that I ended up with using URL routing technique.

So currently my url is like this: mysite.com/article/7737/ It works fine. I get the value of article with {articleid} parameter, defined in my global.asax file.

But I want to get my url something like this: mysite.com/article/article-title-from-database/

is it possible to fetch the title from database and re write url with applying hyphen between each word?

even I am ok if I need to pass two parameters like this: article/{articleid}/{title}/

Following is my Global.asax

<%@ Application Language="C#" %>
<%@ Import Namespace="System.Web.Routing" %>

<script runat="server">
    void Application_Start(object sender, EventArgs e) 
    {
        RegisterRoutes(RouteTable.Routes);
    }
    void RegisterRoutes(RouteCollection routes)
    {
        routes.MapPageRoute("ArticleDetails","article/{articleId}", "~/article/show.aspx");
    }

</script>

And My show.aspx.cs

protected void Page_Load(object sender, EventArgs e)
    {
        if (RouteData.Values["articleId"].ToString() != string.Empty)
        {
            Articles.rptArticle = rptDisplayArticle;
            Articles.rptHashTags = rptHashTags;
            Comments.rptComments = rptDisplayComments;
            Articles.articleId = RouteData.Values["articleId"] as string;
            Articles.Show();
            if (!Comments.show())
            { 
                lblNoComments.Text = "NO COMMENTS";
            }
            if (Authorization.Authorize())
            {
                panCommentPost.Visible = true;
                ltLoginPrompt.Text = "POST A COMMENT";
            }
        }
        else
        { Server.Transfer("~/404.html"); }
askBittu
  • 38
  • 8
  • possible duplicate of [Generate a URL with URL Routing in Webforms](http://stackoverflow.com/questions/279779/generate-a-url-with-url-routing-in-webforms) – Gary Walker Feb 28 '14 at 11:01
  • Well @GaryWalker the solution seems similar to my problem but it looks complicated. I just begun exploring URL routing, so it is all new for me. Any nice explanation / demo / video lecture would be appreciated. – askBittu Feb 28 '14 at 13:14
  • @askBittu You know, whether you think it looks complicated is irrelevant - it is the solution. THe main problem is that ASP.NET Webforms is a really outdated technology that thus has a lot of limitations. Everyone tried to move away many many generations back and is now on MVC - that has nice URL's based into it. – TomTom Feb 28 '14 at 13:32
  • There is no difference between passing id or some text as parameter. But space character is not the only thing you have to worry about, there are others (?*, etc) – Kuzgun Feb 28 '14 at 13:37
  • Dear @Kuzgun I thought of passing title as parameter as well. I can convert title of the article by removing illegal characters and can use it as parameter. But it will create more chances of errors. I am still confused. – askBittu Feb 28 '14 at 13:44
  • I use it like that all the time without errors. The trick is to store url in database in another column. Your title can be Title 1 and url column title-1 – Kuzgun Feb 28 '14 at 14:02
  • @Kuzgun I was about to ask about a similar approach. I was thinking to make another column for alias and when posting an article, according to article's title an alias is generated and saved in the database. Then I can pass it as parameter instead of article id. – askBittu Feb 28 '14 at 14:27
  • If there are more than one similar titles then I will will have to check both the parameters in my show.aspx.cs page. For example: www.mysite.com/article/7737/my-alias-from-database/ will give me 2 parameters. And I will use these 2 parameters to uniquely identify the article. But then there is no need to check alias column. (So not good approach..) – askBittu Feb 28 '14 at 14:34

0 Answers0