Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio confused by server code inside javascript

I ran into an annoying problem: the following code gives a warning in Visual Studio.

<script type="text/javascript">
var x = <%: ViewData["param"] %>;
</script>

The warning is "Expected expression". Visual Studio gets confused, and all the javascript code after that is giving tons of warnings. Granted, it's all warnings, and it works perfectly fine in runtime - but it is very easy to miss real warnings among dozen of false positives.

It was working the same way in VS2008, and it wasn't fixed in VS2010. Does anybody know if there is a workaround, or a patch?

like image 506
Felix Avatar asked Jun 06 '10 07:06

Felix


People also ask

How do I run a JavaScript script in Visual Studio code?

Install the Code Runner Extension. Open the JavaScript code file in Text Editor, then use shortcut Control + Alt + N (or ⌃ Control + ⌥ Option + N on macOS), or press F1 and then select/type Run Code , the code will run and the output will be shown in the Output Window.

How do I Preview JavaScript in Visual Studio code?

Preview on side panel (ctrl+shift+v) : Open preview of HTML on side panel. With this feature, you can easely check the operation of HTML, CSS and JavaScript. Launch on browser (ctrl+shift+l) : Open Web Page on default browser. You can check all operation with web page.

How do I create a JavaScript project in Visual Studio code?

Add a new project file With your project open in Visual Studio, right-click on a folder or your project node in Solution Explorer (right pane), and choose Add > New Item. In the New File dialog box, under the General category, choose the file type that you want to add, such as JavaScript File, and then choose Open.


1 Answers

You need to wrap the server side expression in quotes.

<script type="text/javascript">
var x = "<%: ViewData["param"] %>";
</script>
like image 149
Dustin Laine Avatar answered Sep 24 '22 10:09

Dustin Laine