[ACCEPTED]-How do you escape a '@' symbol within in a url with razor-razorengine

Accepted answer
Score: 30

Try use @ instead of an actual @

<a href="https://www.google.co.uk/maps/place/XXXXXXXXXXXXXXXX/&#64;55.000000,-1.000000,17z/data=!3m1!4b1!4m2!3m1!1s0x487e736c74d13649:0xe560f3b38693aec3">View on Google Maps</a>

0

Score: 8

I wonder why nobody suggest using url encoded 1 character %40 for @?

<a href="https://www.google.co.uk/maps/place/XXXXXXXXXXXXXXXX/%4055.000000,-1.000000,17z/data=!3m1!4b1!4m2!3m1!1s0x487e736c74d13649:0xe560f3b38693aec3">View on Google Maps</a>

For me this works.

http://meyerweb.com/eric/tools/dencoder/

Score: 5

Just another way:

<a href="https://www.google.co.uk/maps/place/XXXXXXXXXXXXXXXX/@("@55.000000,-1.000000"),17z/data=!3m1!4b1!4m2!3m1!1s0x487e736c74d13649:0xe560f3b38693aec3">View on Google Maps</a>

or

<a href="https://www.google.co.uk/maps/place/XXXXXXXXXXXXXXXX/@("@")55.000000,-1.000000,17z/data=!3m1!4b1!4m2!3m1!1s0x487e736c74d13649:0xe560f3b38693aec3">View on Google Maps</a>

0

Score: 1

I found another (may in some case better) way 7 to escape @-symbols in razor templates.

In 6 my usecase, I have a partial with assets 5 wich should be replaced by a usemin grunt-task. When 4 reference a scoped npm package, there is 3 a @ inside the path string.

@using Foo.Bar.Helpers

@{ 
    var somescope = "@somescope";
}

@Html.RegisterAssetBlock(
    content: @<text>
        <!-- build:js /assets/js/bundle.js -->
        <script src="/node_modules/@somescope/somepackage/dist/main.js" type="text/javascript"></script>
        <!--endbuild-->
    </text>
)

So in every case, there 2 is the correct string (client compile-time and 1 server compile-time).

For your case this would mean following:

@using Foo.Bar.Helpers

@{
    var location = "@55.000000,-1.000000";
}

<a href="https://www.google.co.uk/maps/place/XXXXXXXXXXXXXXXX/@(location),17z/data=!3m1!4b1!4m2!3m1!1s0x487e736c74d13649:0xe560f3b38693aec3">View on Google Maps</a>
Score: 0

The simplest fix for this is to use @@ instead 2 of @.

This is the way MS recommends to escape 1 a single @.

More Related questions