2

Issue using an @ sign in src path of a script reference.

I am using a .cshtml page so @ is a code reference. Normally @@ will cancel out the @ sign so it will read it as a string but it doesn't seem to work in the following example.

<script>
    var signalrPath = "~/lib/@@microsoft/signalr/dist/browser/signalr.js";
</script>

<script src="~/lib/@@microsoft/signalr/dist/browser/signalr.js"></script>

This translates to the following:

enter image description here

Is there any easy way to avoid this?

I can make a cshtml string object and insert it in to there but that seems unnecessary.

@{ 
    string signalR = "@microsoft";
}

<script src="~/lib/@signalR/signalr/dist/browser/signalr.js"></script>

1 Answer 1

4

Use an explicit Razor expression:

<script>
    var signalrPath = "@("~/lib/@microsoft/signalr/dist/browser/signalr.js")";
</script>

<script src="@("~/lib/@microsoft/signalr/dist/browser/signalr.js")"></script>

Or, I suppose you can just do the @microsoft part, but I personally prefer keeping the whole string together:

<script src="~/lib/@("@microsoft")/signalr/dist/browser/signalr.js"></script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.