DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

ASP.NET MVC: Defining short URL-s for root level pages

Gunnar Peipman user avatar by
Gunnar Peipman
·
Apr. 17, 11 · · News
Like (0)
Save
Tweet
23.62K Views

Join the DZone community and get the full member experience.

Join For Free

Short URL-s are more and more important part of page usability as mobile internet is growing. Long URL-s are not convenient to type due to small keyboards and screens of mobile devices. Also short URL-s are easier to remember and using well chosen short URL-s your pages may also get better rankings in search engines indexes. In this posting I will show you how to create short URL-s for ASP.NET MVC pages.

Default routes

Routes used by default are part of ASP.NET MVC applications right from start. After creating new ASP.NET MVC application you can find the following RegisterRoutes() method from Global.asax file.

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  
    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index",
             id = UrlParameter.Optional } // Parameter defaults
    ); 
}

IgnoreRoute() call here sais to ASP.NET MVC that *.axd files need no processing. Call of MapRoute() method creates new route and sets the following default values:

  • controller – if controller name cannot be found from URL then take HomeController,
  • action – if action cannot be found from URL then take Index() as default action,
  • id – it is optional and may be omitted.

We can define other routes too if we need.

Shortening URL of About page

As all ASP.NET MVC applications get by default also page called default we can use it to analyze its default URL and create shorter URL after that. By default, About page has the following URL:

http://yourhost/Home/About

As Home controller is all about “default” pages as activities that happen on root level of site we don’t have any good reason to keep word Home in URL. In SEO terms it increases the distance of page title and in some cases shorter URL-s perform better in search engines.

To get short URL like this:

http://yourhost/about

add the following route definition to previously shown method, right before last route definition.

routes.MapRoute(
    "ShortAbout",
    "about",
    new { controller = "Home", action="about" }
);

When you run your application you can see that ASP.NET MVC generates short URL for About page automatically. 

This solution works well if you have only some pages under root level of site. This is default case for many pages. Of course, it is possible to write more code and have more advanced routing that handles all short URL cases but right now I think this way you move quicker.

Conclusion

ASP.NET offers powerful routing mechanism and MVC framework makes good use of it. Sometimes we may need better URL-s for root level pages than the ones that are generated by MVC framework by default. There may be pages like About, Terms, Join, Log in etc. If we don’t have many pages on root level we can easily define new routes for these pages that offer shorter URL-s that are easier to remember and easier to type on mobile devices. Also search engines may give better rankings to our pages if we don’t use longer URL-s. We saw that it was easy to add new routes to routes collection for pages that need shorter URL. Our method works as far as there are not much pages on root level of site.

ASP.NET MVC ASP.NET Data Types

Published at DZone with permission of Gunnar Peipman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Refactoring Java Application: Object-Oriented And Functional Approaches
  • How to Leverage Method Chaining To Add Smart Message Routing in Java
  • Modern REST API Design Principles and Rules
  • After COVID, Developers Really Are the New Kingmakers

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo