3

Every time I put an inline tag on the link's href attribute it somehow gets auto-encoded. Is this ASP.NET's default behavior? How can I dynamically set the Href attribute using code? This is in MVC btw.

Attempted something like this

<link href="<%: Link.Content.Jquery_css %>" rel="stylesheet" type="text/css" />

which rendered this (nothing changed)

<link href="<%: Link.Content.Jquery_css %>" rel="stylesheet" type="text/css" />

and this

<link href="<%= Link.Content.Jquery_css %> rel="stylesheet" type="text/css" />

which produced this (I couldn't remember the exact numbers, but it seems the bracket-percent-equals was encoded to link format)

<link href="/View/Shared%25Link.Content.Jquery_css%25" %>" rel="stylesheet" type="text/css" />

Link.Content.Jquery_css is a strongly typed string containing the link made using T4MVC.

Add'l info: I used ASP.NET MVC 2, in .NET 4 and testing in Firefox.

4
  • what does Link.Content.Jquery_css look like when you do Response.Write(Link.Content.Jquery_css)? Is it being encoded before it hits the view? Commented Nov 24, 2010 at 0:27
  • Seems like it is. I'm just confused since I'm using the same Link.Content class for a javascript reference on the same master page <script src=<%= Link.Content.Jquery_js%> /> and it's working as expected. Commented Nov 24, 2010 at 0:37
  • You'll have to trace back to the creation of that element. Can you show some code from either the Model or the Controller where the Jquery_css is being created? Commented Nov 24, 2010 at 1:07
  • If I'm not mistaken, it's a static readonly string. I don't have the source code at the moment. I'll post additional details if things still don't work later. Commented Nov 24, 2010 at 1:10

5 Answers 5

6

I got the same issue in the master page. (It doesn't happen in an individual page.) I found removing "runat=server" from the head tag fixed the problem.

Sign up to request clarification or add additional context in comments.

1 Comment

+1, my issue was with webforms, but removing runat="server" did fix up the exact problem the OP had (with it not interpreting the code)
4

It's getting auto-encoded because of the tag your using (<%: %>). If you don't want the URL to be Encoded, use the following:

<link href="<%= Link.Content.Jquery_css %>" rel="stylesheet" type="text/css" />

1 Comment

I forgot to say that I've tried it already. Edited my question to clarify it.
3

Change the ":" to "=" and that remove the auto encoding

Comments

1

Your view is unable to access Link.Content.Jquery_css property. ASP.NET unhelpfully doesn't throw any error.

Move that line inside the body of the page and you will see compilation error.

Comments

1

You could do this:

<head>
    <style type="text/css">
        @import "<%= ResolveUrl("~/content/styles.css") %>";
        @import "<%= ResolveUrl("~/content/print.css") %>" print;
    </style>
</head>

1 Comment

Cool! I should have thought about this! All the above ideas didn't work. Not to mention that runat="server" isn't even relevant here (MVC). Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.