ASP.NET MVC 2 And MVC 3 Relative Path

Sometime, you just need to work with MVC 2 and have no choice although MVC 3 is out there. Here’s another MVC 2 tip on how to use relative path.

This is especially useful (and important) when deploy to IIS. What I’d seen is, the CSS / images / javascript / jquery would work in local machine, but not in IIS. Most likely it’s because the path to CSS / images / javascript / jquery files in your Site.Master / *.aspx / *.ascs / *.cshtml.

  1. For master page, add the following page directive in Site.Master:
    <%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
  2. For *.aspx pages, make sure you have the following page directive (you might / might not have MasterPageFile attribute):
    <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
  3. The relative path format (for master page and *.aspx) should look like:
    <link href="<%=  Url.Content("~/Content/Site.css")%>" media="all" rel="stylesheet" type="text/css" />
  4. The relative path format (*.cshtml) should look like (note that *.cshtml doesn’t need any page directive defined):
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />

2 thoughts on “ASP.NET MVC 2 And MVC 3 Relative Path”

  1. That is very interesting, You are an excessively professional blogger. I’ve joined your rss feed and look ahead to in search of extra of your magnificent post about click here. Also, I have shared your website in my social networks
    Hello There. I discovered your weblog the usage of msn. That is an extremely neatly written article. I will be sure to bookmark it and return to learn extra of your useful information. Thank you for the post. I’ll definitely return.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s