Welcome to William's ekasi

[ Log On ]
Quick Asp.Net Mvc <head> element gotcha
2010/01/01
Tags:

<head runat=”server”> doesn’t go through normal asp.net processing. So when you use something like

<link href=’<%= Url.Content(“~\Content\Site.css”) %>’ rel=’stylesheet’ type=’text/css’ />

You’ll end up with something like

<link rel="stylesheet" type="text/css" href="../Views/Shared/%3C%25=%20Url.Content(%22~/Content/Site.css%22)%25%3E" />

which clearly isn’t what we wanted.  The reason for this is the <link /> elements have special processing in Asp so the <% %> code nuggets don’t work.  Thankfully it’s a quick fix:

Take advantage of the special processing and simply set the source to href=’~/Content/Site.css’.  The processing engine will resolve the url correctly.

<link href=’~/Content/Site.css’ rel=’stylesheet’ type=’text/css’ />

That will resolve to what we expect.

Add Comment

No comments have been posted yet