dedicated to write powerful yet elegant software by using minimum code and maximizing the benefits for all our clients.

Referencing external files in MVC Views by Sunny 14/Jun/2009 21:05:00

Typically, new developers to ASP.Net MVC get stumped with having to deal with one important fact: Page.ResolveUrl(...); is missing!!!

Don't worry or fret - the new way of doing this is:Url.Content(...);

Some sample usage is as below:

Resolving images:

<img src="<%= Url.Content("~/content/images/myImage.gif") %>" alt="My image" title="My image" />

Resolving JavaScript files:

<script language="javascript" type="text\javascript" src="<%= Url.Content("~/scripts/myscript.js") %>"></script>

Simple as that.

When you refer stylesheet files in your *.Master page, this link is directly reference without needing to use the Url helper as shown above.

So, you can continue refering stylesheet files like:

<link rel="stylesheet" type="text\css" media="screen" href="../content/site.css" />

The MVC framework will resolve the path correctly at run-time.

Hmm.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Category: Development | ASP.Net MVC

Comments are closed