Understanding the runat server attribute

You asked why the styles are not applied anymore when removing the runat="server" from the<head> element.

It is simple: by running on the server side, the parser will replace the ~/ from the stylesheet declaration <link href="https://stackoverflow.com/questions/11510502/~/Styles/Site.css" rel="stylesheet" type="text/css" /> with the root path of the application.

The ~ is illegal in a URL. Thus, if this is not replaced by the parser, the file will not be found thus the stylesheet will not be applied.

Oh, btw, setting the runat="server" attribute on the <head> element will force all its sub-elements to be run on the server, thus why the <link> element is run on the server.

Leave a Comment