Guy Norton
Guy Norton

Reputation: 135

HtmlHelper encodes & as &

How do I stop the & in the following string from being encoded as & amp;

public static class GoogleMaps
{
    public static MvcHtmlString DisplayMap(this HtmlHelper html,
                        GoogleMapData mapData, int width, int height)
    {
        const string GOOGLE_MAP_API = "xxx";
        const string GOOGLE_MAP_DOMAIN = "www.google.com/maps";
        const string GOOGLE_MAP_ACTION = "/embed/v1/view";

        var tag = new TagBuilder("iframe");
        tag.MergeAttribute("width", width.ToString());
        tag.MergeAttribute("height", height.ToString());
        tag.MergeAttribute("style", "border:0");
        tag.MergeAttribute("frameborder", "0");

        string googleMapUrl = String.Format(
                                @"{0}://{1}{2}?key={3}&center={4}",
                                "https",
                                GOOGLE_MAP_DOMAIN,
                                GOOGLE_MAP_ACTION,
                                GOOGLE_MAP_API,
                                mapData.Coordinates.ToString()
                                            );
        tag.MergeAttribute("src", googleMapUrl);

        return MvcHtmlString.Create(tag.ToString());

    }
}

This generates an Iframe with an SRC such as this:

src="https://www.google.com/maps/embed/v1/view?key=myAPIkey&center=52.377736, 4.915779"

GoogleMaps ignores center (GPS coordinates)

What I want is this:

src="https://www.google.com/maps/embed/v1/view?key=myAPIkey&center=52.377736, 4.915779" 

Upvotes: 2

Views: 768

Answers (2)

jadavparesh06
jadavparesh06

Reputation: 946

You can try this with System.Web.HttpUtility.HtmlDecode(tag.Tostring()) to generate the Html tag string and then use this string in MvcHtmlString.Create() routine.

Upvotes: 1

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93424

The HtmlHelper is supposed to encode the ampersand. In fact, the ampersand when used in an attribute is supposed to be encoded to be valid HTML. The W3C validator will tell you the same thing.

Your code will work just fine, the browser will understand it, and so will the server.

Don't believe me? See these links:

(specifically "A link in HTML (or any HTML attribute value") http://mrcoles.com/blog/how-use-amersands-html-encode/

http://www.htmlhelp.com/tools/validator/problems.html

Upvotes: 1

Related Questions