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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Frameworks
  4. ASP.NET MVC & Url Extensions

ASP.NET MVC & Url Extensions

Dane Morgridge user avatar by
Dane Morgridge
·
Jul. 27, 10 · Interview
Like (0)
Save
Tweet
Share
13.49K Views

Join the DZone community and get the full member experience.

Join For Free
We got a really cool feature called extension methods in C# 3.0.  This allowed a developer to add methods to existing classes, even sealed ones.  This effectively makes any class extensible even base classes like System.String.  This MSDN article explains in more detail what extension methods are and gives some good examples of adding a WordCount method to the System.String class.  What I want to show you is how I use extension methods to make my life easier when working with ASP.NET MVC.

I use extension methods in ASP.NET MVC probably more than any other project type I work on. There is a static class called simply "Url" which is used to provide some url helpers.  In ASP.NET Webforms, you could prepend a link with "~/" to ensure it was relative to the root of the application as opposed to being relative to the current URL.  With the RESTful nature of URLs with ASP.NET MVC, this can be important to make sure that links function properly and resources like images and CSS files are referenced properly. Using "~/" is specific to ASP.NET and does't work with standard HTML although there are some techniques that can provide the same results with only HTML.

ASP.NET MVC will do some re-writing behind the scenes when referencing links. So let's say that you have a style.css file in the ~/Content folder of your application and you want to reference if from the ~/Views/Shared/Site.Master master page file.  The HTML you can put in the master page to reference the file will look like:
<link href="../../Content/Site.css" rel="stylesheet" type="text/css"/>
Because the physical path of the Site.Master file is two levels deep from the root, using "../../" works.  When the page is built, the ASP.NET runtime will figure out all of the references for you based on the path levels you supply.  However, if you don't use "../../" it won't work.  To get the "~/" goodness, you can use Url.Content and pass in the location of the resource as such:
<link href="<%= Url.Content("~/Content/Site.css")%>" rel="stylesheet" type="text/css"/>
This will work but we can make it more descriptive using extension methods.  Let's add a Style method to the Url class that takes in the stylesheet name.  I'll start by adding a new UrlExtensions static class and a static method StyleSheet:
public static class UrlExtensions
{
public static string StyleSheet(this UrlHelper helper, string fileName)
{
return helper.Content(string.Format("~/Content/{0}", fileName));
}
}
Extension methods must be static and the first parameter has to be the class that you are wanting to extend.  In this case the UrlHelper class is what we want to add to and it is the type that "Url" is an instance of.  I am using the helper's Content method but passing in only the part that changes.  The extension method will take the filename of our stylesheet so our new stylesheet link will look like:
<link href="<%= Url.StyleSheet("Site.css") %>" rel="stylesheet" type="text/css"/>
What else is kind of cool is if you decide you want to change the overall location of your CSS files, you only need to change the extension method. I usually create one for images and JavaScript files as well.  I also commonly use this method when creating links.  If you look back to my article on routing in ASP.NET MVC, I used a privacy policy as an example.  I can use an extension method to create a url to use in a link:
public static string Privacy(this UrlHelper helper)
{
return helper.Content("~/privacy");
}
Now I can build a link to the page:
<a href="<%= Url.Privacy() %>">Privacy Policy</a>
I can use named routes in this case and use an action link, but for links that may not have their own route, this is a great method of creating URLs.  It makes your application more maintainable because these links may be in multiple places and you would only have to change it in one place.
ASP.NET ASP.NET MVC

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • A First Look at Neon
  • How Chat GPT-3 Changed the Life of Young DevOps Engineers
  • Master Spring Boot 3 With GraalVM Native Image
  • Java Concurrency: LockSupport

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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: