20

I have a URL I would like to render in an anchor tag as-is in a Razor view. I would have thought Html.Raw would be the way to go:

@{
    string test = "http://someurl.com/someimage.png?a=1234&b=5678";
}

<div>
    <a href="@Html.Raw(test)">Test</a>
</div>

But this doesn't work. The ampersand gets encoded and the HTML is rendered as:

<div>
    <a href="http://someurl.com/someimage.png?a=1234&amp;b=5678">Test</a>
</div>

The strange thing is that if I do the Html.Raw call outside of the anchor tag, the HTML output is as expected:

<div>
    @Html.Raw(test)
<div>

is rendered as:

<div>
    http://someurl.com/someimage.png?a=1234&b=5678
</div>

Can anyone explain why this is?

Edit:

Tried a few other things out, and found that the following works as expected:

<div data-url="@Html.Raw(test)"></div>

outputs:

<div data-url="http://someurl.com/someimage.png?a=1234&b=5678"></div>

To add a little more context, my goal is not actually to use the URL in an anchor tag, since hrefs can be HTML encoded and still work (I just used the anchor tag case as an example). Rather I wish to use the URL in an <object> <param> value tag for a Flash object. The Flash object doesn't properly recognize the HTML encoded URL, but I can't get the raw URL to output correctly.

I am at a loss. Time to break out the MVC source code I guess...

Nick Larsen
  • 18,631
  • 6
  • 67
  • 96
Joe Waller
  • 1,971
  • 3
  • 20
  • 25
  • possible duplicate of [Why is MVC 4 Razor escaping ampersand when using HTML.Raw in a title attribute](http://stackoverflow.com/questions/12321616/why-is-mvc-4-razor-escaping-ampersand-when-using-html-raw-in-a-title-attribute) – Martin Brown Mar 27 '15 at 15:04

5 Answers5

12

This is happening because something in the pipeline (I'd guess razor but I'd need to look it up) attribute encodes all attribute values. This should not affect the browser from reaching your desired location however.

You can test this with the @Html.ActionLink(text, action, routeAttributes) overload.

@Html.ActionLink("Test", "Index", new { tony = "1", raul = 2 })

outputs

<a href="/?tony=1&amp;raul=2">Test</a>

In regards to your edit, you just need to make the entire <param> part of your raw value.

@{
    var test = "<param src=\"http://someurl.com/someimage.png?a=1234&b=5678\">";
}

<div>
    <object>
    @Html.Raw(test)
    </object>
</div>
Nick Larsen
  • 18,631
  • 6
  • 67
  • 96
  • Is this answer up to date? I just tried the `@Html.ActionLink("Test", "Index", new { tony = "1", raul = 2 })` in VS2013, MVC5 and the output was `Test` ... – Michal Hosala Nov 11 '15 at 00:23
4

Like this:

@{
    string test = "http://someurl.com/someimage.png?a=1234&b=5678";
}

<div>
    <a href="@Html.Raw(Html.AttributeEncode(test))">Test</a>
</div>

produces valid markup:

<div>
    <a href="http://someurl.com/someimage.png?a=1234&amp;b=5678">Test</a>
</div>

But this doesn't work. The ampersand gets encoded and the HTML is rendered as:

But that's exactly how a valid markup should look like. The ampersand must be encoded when used as an attribute. Don't worry, the browser will perfectly fine understand this url.

Notice that the following is invalid markup, so you don't want this:

<div>
    <a href="http://someurl.com/someimage.png?a=1234&b=5678">Test</a>
</div>
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • The example is misleading, he is trying to use an `` setup for a flash object and he thinks it's not picking up the value correctly (maybe double encoding once in flash?) – Nick Larsen Aug 15 '12 at 17:33
  • @NickLarsen, but the following is **invalid markup**: ``. Try any HTML validator and it will tell you this. Why would someone want to produce invalid markup? If the Flash movie doesn't understand valid markup maybe this flash movie requires to be fixed. – Darin Dimitrov Aug 15 '12 at 17:50
  • that's a good point unless he doesn't have access to change the flash part. – Nick Larsen Aug 15 '12 at 17:59
  • @NickLarsen, in this case I would contact the authors of this flash plugin to notify them of this bug and ask them to fix it. I would prefer that rather than writing invalid markup on my website. – Darin Dimitrov Aug 15 '12 at 18:01
  • I apologize for the misleading example. I realize the markup would be invalid, but Nick is right in that I do not have access to the flash part, and getting the authors to move on this has not gone as smoothly as I'd hoped. I suppose I should have included this information in the original question. – Joe Waller Aug 16 '12 at 02:42
  • 3
    Oddly, your example might be exposing a bug in Razor that [others are experiencing](http://stackoverflow.com/q/12321616/48700). It doesn't appear to produce the expected markup. If you view the source on the result, it is being encoded twice: `Test`. – patridge Sep 07 '12 at 18:08
3

Try

<div>
    <a href="@MvcHtmlString.Create(test)">Test</a>
</div>
  • No luck. I get the same behavior as Html.Raw(). – Joe Waller Aug 15 '12 at 02:54
  • @JoeWaller Did not work for me at first but after I updated Razor and MVC to latest version (which was a pain because it broke things...) then both Html.Raw() and MvcHtmlString.Create() did work. – bets May 24 '17 at 12:26
1

just try below... It works perfect.

@{
    string test = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token="+@ViewBag.Token;
}
<div>
    <a href="@Html.Raw(System.Web.HttpUtility.HtmlDecode(test))">@Html.Raw(System.Web.HttpUtility.HtmlDecode(test))</a>
</div>
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
0

why don't you use jquery to post the url:

    $(function () {
    $('form').submit(function () {
        $.ajax({
            url: this.action,
            type: this.method,
            data: { a:'firstvalue', b: 'secondvalue'},
            success: function (result) {
//code for successful result
            }
        });
        return false;
    });
});

in controller

public ActionResult Fetch(string a, string b)
    {
        //write required codes here
            return View();

    }
Suraj Shrestha
  • 1,790
  • 1
  • 25
  • 51