Sunteți pe pagina 1din 3

NL NP NL

` = ~~ r o i == p mK b q = = ~ o = ^ k t m~= W = = ^ p mK b q = p == q l ~j = k p

Search ASP.NET

Sign In | Join

Home

Get Started

Downloads

Web Pages

Web Forms

MVC

Solutions

Community

Forums

Overview

Tutorials

Videos

Samples

Forum

Books

Open Source
Build powerful and elegant apps with the tools you trust. Learn How

Home / Web Pages / Tutorials / Chapter 3. Working with Pages / Creating Readable URLs in ASP.NET Web Pages Sites

Creating Readable URLs in ASP.NET Web Pages (Razor) Sites


By Microsoft ASP.NET Team | May 21, 2012

TABLE OF CONTENTS This article describes routing in an ASP.NET Web Pages (Razor) website, and how this lets you use URLs that are more readable and better for SEO. What you'll learn: How ASP.NET uses routing to let you use more readable and searchable URLs. Introducing ASP.NET Web Pages 2 Introducing ASP.NET Web Pages 2 Getting Started Introducing ASP.NET Web Pages 2 Programming Basics Introducing ASP.NET Web Pages 2 Displaying Data Introducing ASP.NET Web Pages 2 HTML Form Basics Introducing ASP.NET Web Pages 2 Entering Database Data by Using Forms Introducing ASP.NET Web Pages 2 Updating Database Data Introducing ASP.NET Web Pages 2 Deleting Database Data Introducing ASP.NET Web Pages 2 Creating a Consistent Layout Introducing ASP.NET Web Pages 2 Publishing a Site by Using WebMatrix Basics Intro to ASP.NET Web Programming Razor Syntax ASP.NET Web Pages Visual Basic Intro to Debugging Working with Pages Working with Forms Validating User Input in ASP.NET Web Pages Sites Creating a Consistent Look Customizing Site-Wide Behavior Creating and Using a Helper in an ASP.NET Web Pages Site Rendering ASP.NET Web Pages Sites for Mobile Devices Creating Readable URLs in ASP.NET Web Pages Sites Data Working with Data Displaying Data in a Chart NL P

Note The information in this article applies to ASP.NET Web Pages 1.0 and Web Pages 2, and to Microsoft WebMatrix 1.0 and Microsoft WebMatrix 2 RC.

About Routing
The URLs for the pages in your site can have an impact on how well the site works. A URL that's "friendly" can make it easier for people to use the site. It can also help with search-engine optimization (SEO) for the site. ASP.NET websites include the ability to use friendly URLs automatically. ASP.NET lets you create meaningful URLs that describe user actions instead of just pointing to a file on the server. Consider these URLs for a fictional blog: http://www.contoso.com/Blog/blog.cshtml?categories=hardware http://www.contoso.com//Blog/blog.cshtml?startdate=2009-11-01&enddate=2009-11-30 Compare those URLs to the following ones: http://www.contoso.com/Blog/categories/hardware/ http://www.contoso.com/Blog/2009/November In the first pair, a user would have to know that the blog is displayed using the blog.cshtml page, and would then have to construct a query string that gets the right category or date range. The second set of examples is much easier to comprehend and create. The URLs for the first example also point directly to a specific file (blog.cshtml). If for some reason the blog were moved to another folder on the server, or if the blog were rewritten to use a different page, the links would be wrong. The second set of URLs doesn't point to a specific page, so even if the blog implementation or location changes, the URLs would still be valid. In ASP.NET Web Pages, you can create friendlier URLs like those in the above examples because ASP.NET uses routing. Routing creates logical mapping from a URL to a page (or pages) that can fulfill the request. Because the mapping is logical (not physical, to a specific file), routing provides great flexibility in how you define the URLs for your site.

How Routing Works


When ASP.NET processes a request, it reads the URL to determine how to route it. ASP.NET tries to match individual segments of the URL to files on disk, going from left to right. If there's a match, anything remaining in the URL is passed to the page as path information. ~K J~L ~ J L L J~L ~ ~~ J J J J~J J~J

NL NP NL

` = ~~ r o i == p mK b q = = ~ o = ^ k t m~= W = = ^ p mK b q = p == q l ~j = k p Migrating a Database to SQL Server Files, Images, and Media Working with Files Working with Images Working with Video Displaying Maps in an ASP.NET Web Pages Site Security Adding Security and Membership Adding Security to Any Site Enabling Login from External Sites in an ASP.NET Web Pages Site Using a CAPTCHA to Prevent Automated Programs (Bots) from Using Your ASP.NET Web Site Email and Search Sending Email from Your Site Adding Search to Your Web Site

Imagine that someone makes a request using this URL: http://www.contoso.com/a/b/c The search goes like this: 1. Is there a file with the path and name of /a/b/c.cshtml? If so, run that page and pass no information to it. Otherwise ... 2. Is there a file with the path and name of /a/b.cshtml? If so, run that page and pass the value cto it. Otherwise 3. Is there a file with the path and name of /a.cshtml? If so, run that page and pass the value b cto it. / If the search found no exact matches for .cshtml files in their specified folders, ASP.NET continues looking for these files in turn: 4. /a/b/c/default.cshtml (no path information). 5. /a/b/c/index.cshtml (no path information).

Note To be clear, requests for specific pages (that is, requests that include the .cshtml filename extension) work just like you'd expect. A request like http://www.contoso.com/a/b.cshtml will run the page b.cshtml just fine.

Inside a page, you can get the path information via the page's U l a aproperty, which is a dictionary. Imagine that rDt you have a file named ViewCustomers.cshtml and your site gets this request: http://mysite.com/myWebSite/ViewCustomers/1000 As described in the rules above, the request will go to your page. Inside the page, you can use code like the following to get and display the path information (in this case, the value "1000"): <DCYEhm> !OTP tl <tl hm> <ed ha> <il>RDt<tte tteULaa/il> <ha> /ed <oy bd> Csoe I:@rDt[]TSrn( utmr D Ulaa0.otig) <bd> /oy <hm> /tl

Social Networking Adding Social Networking to Your Website Performance and Traffic Caching to Improve the Performance of Your Website Analyzing Traffic Bundling and Minifying Assets in an ASP.NET Web Pages (Razor) Site

Translate this page


Spanish

Microsoft Translator

Note Because routing doesn't involve complete file names, there can be ambiguity if you have pages that have the same name but different file-name extensions (for example, MyPage.cshtml and MyPage.html). In order to avoid problems with routing, it's best to make sure that you don't have pages in your site whose names differ only in their extension.

Additional Resources
WebMatrix - URLs, UrlData and Routing for SEO. This blog entry by Mike Brind provides some additional details on how routing works in ASP.NET Web Pages.

By Microsoft ASP.NET Team, ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.

Comments (0)

You must be logged in to leave a comment.

~K J~L ~ J L L J~L ~ ~~ J J J J~J J~J

OP L

NL NP NL

` = ~~ r o i == p mK b q = = ~ o = ^ k t m~= W = = ^ p mK b q = p == q l ~j = k p

This site is hosted for Microsoft by Neudesic, LLC. | 2012 Microsoft. All rights reserved. Privacy Statement | Terms of Use | Site Feedback | Advertise With Us

Follow Us On: Twitter | Facebook Feedback on ASP.NET | File Bugs

~K J~L ~ J L L J~L ~ ~~ J J J J~J J~J

PL P