Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force razor to not escape a url

Razor's auto-escape is messing with me. I'm building a url to feed to jquery's $.load method:

<script>
$("#baz").load('@Url.Action("Method", new { foo=Model.Foo, bar=Model.Bar })');
<script>

The problem is, the url is coming out escaped in the emitted script (& == &)

<script>
$("#baz").load('Method?foo=Foo&amp;bar=Bar');
<script>

So MVC chokes on the request saying that the bar parameter was not provided.

I've tried wrapping the call up in @Html.Raw but that still seems to escape the url. Back on planet aspx, I'd just do <%= instead of <%: but obviously that's no good here :). I also tried putting the parameters in a data object for $.load, but that seems to force a post, not a get (and I want a get here).

This is something I could just fix with routing, but this project's not there yet -- still getting functionality down without messing with routing. On the flip side, I need to be able to eventually route this url, so I don't want to just hardcode the querystring.

Any thoughts?

like image 755
lamont Avatar asked Oct 20 '11 18:10

lamont


1 Answers

I just ran a test, and the ampersand was not encoded in my test using Html.Raw, like so:

@Html.Raw(Url.Action("Method", new { foo = Model.Foo, bar = Model.Bar }))
like image 87
counsellorben Avatar answered Nov 03 '22 00:11

counsellorben