Geeks With Blogs

@azamsharp
  • azamsharp The WWDC app says that there are new videos but there is nothing under videos!!! about 507 days ago
  • azamsharp Can I post my opinion on iOS 7 Beta or is it under NDA or something? about 507 days ago
  • azamsharp iOS 7 BETA installed successfully! about 507 days ago
  • azamsharp iOS 7 BETA installed! Now restoring! I am on NET10 which uses AT&T network. Hopefully 3G will still work! Lets c.. about 507 days ago
  • azamsharp The missing of button borders in Xcode 5 makes it hard to determine the clickable area. about 507 days ago
  • azamsharp I might wait for iOS to be released before I can put it on my device! Don't want a bricked or unstable device! about 507 days ago
  • azamsharp Android has many features that no one uses, number of versions that no one upgrades to and thousands of apps that no one pays for. about 507 days ago
  • azamsharp Maybe it is just me but the new iOS 7 UI looks like Windows Phone 8 UI! #maybeIamCrazy about 508 days ago
  • azamsharp Anyone using H20 network upgraded to iOS 7 BETA Keep me updated if you face any problems. about 508 days ago
  • azamsharp @merowing_ You already downloaded it! I cannot even load the developer's website! :( about 508 days ago

AzamSharp Some day I will know everything. I hope that day never comes.

Few days ago I wrote a post about Url Rewriting Using RewritePath Method. The basic idea was to rewrite the path to the correct one inside the Application_BeginRequestPath method. This worked out okay but the string manupilation was a bit ugly.

ASP.NET MVC Framework gives you the complete control over the URL's. So, you can easily change the URL to anything you like. Let's say I have a url like the following:

Articles/[id]

and I like to to display the URL like this:

Articles/[id]_[title of the article]

For this to work we need to add a new route entry in the route table as shown below:

RouteTable.Routes.Add(new Route
            {
                Url = "Articles/[title]",
                Defaults = new { controller = "Articles", action = "Details", title = (string)null },
                RouteHandler = typeof(MvcRouteHandler)
            });

In the code above I have added the title anonymous type to be displayed in the URL. Now, I need to recreate the URL so when the user clicks the artilcle he/she is redirected to the correct article. Here is the page that list all the articles.

 <% foreach (var article in ViewData)
       {
          
      %>
     
      <li>
      <%= Html.ActionLink(article.Title, new { controller = "Articles", action = "Details", title = (article.ArticleID+"_"+article.Title).Replace(" ","_")
         }) %>
      </li>
      
       <% } %>

The title is generated using the ArticleID and the Title fields. It should look something like this:

Articles/120_Introduction_To_C_Sharp_Programming

Now, let's see the Details action which is triggered when the article is clicked.

 [ControllerAction]
        public void Details(string title)
        {

            int id = Int32.Parse(title.Split('_')[0]);
            var article = ArticleRepository.GetById(id);           
            RenderView("Details", article);            
        }

I extracted out the id from the URL and passed to the ArticleRepository.GetById method to get the particular article.

The ASP.NET MVC Framework gives us the flexibility to create our own URL's and hence the more informative the URL is the better chances it has to be picked by the search engines.

Posted on Wednesday, January 30, 2008 10:42 AM | Back to top


Comments on this post: Url Rewriting Using ASP.NET MVC Framework

# re: Url Rewriting Using ASP.NET MVC Framework
Requesting Gravatar...

Url Rewriting Using ASP.NET MVC Framework is good service but it have no value for search engine.
Left by 70-400 on Dec 01, 2010 11:31 PM

Your comment:
 (will show your gravatar)
 


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net | Join free