Uzair Khan
Uzair Khan

Reputation: 2970

Using @ inside href of anchor tag

I have implemented a simple payment gateway, the payment url has @ in it, as shown below:

<a href="https://www.somegateway.com/@john_doe/" class="im-checkout-btn btn--light" target="_blank" rel="modal">Checkout With someGateway</a>

This is an ASP.NET MVC web application, so the view is Razor view engine, and it is not accepting @ inside the href, it gives me:

CS0103: The name 'john_doe' does not exist in the current context

but when I simply create a html page and use the above code it works, it just doesn't work in razor view. Where am I going wrong?

Upvotes: 0

Views: 992

Answers (2)

Peter B
Peter B

Reputation: 24280

You can put the url in a variable and then use that:

@{
    var url = "...";
}
<a href="@Html.Raw(url)">...</a>

Or even without the variable:

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

Upvotes: 1

Tarek
Tarek

Reputation: 1279

You need to escape the @ character in razor you can do that by using @@ instead. But that doesn't work all the time inside links despite the documentation.

Writing your link like this should work!

<a href="https://www.somegateway.com/@("@john_doe")/" class="im-checkout-btn btn--light" target="_blank" rel="modal">Checkout With someGateway</a>

Or

<a href="https://www.somegateway.com/@Html.Raw('@')john_doe/" class="im-checkout-btn btn--light" target="_blank" rel="modal">Checkout With someGateway</a>

If all else fails you could do

@{ var url = "https://www.somegateway.com/@john_doe/"; }

then

<a href="@url" class="im-checkout-btn btn--light" target="_blank" rel="modal">Checkout With someGateway</a>

Upvotes: 2

Related Questions