Partilhar via


What's the deal with "

" when generating client script from ASP.NET?

Why I can't use "</script>" in my C# code when constructing client script blocks? Why do I have to split "</script>" string into two chunks like

string myScript = "<script language='javascript'> + "</" + "script>";

Like recommended, for example, here or here? (See also this bug). Why this snippet:

<script runat="server">

string buildScript()
{
   return "<script language='javascript'></script>";
}

</script>

was fine in VS 2003 HTML editor, but produces an error in VS 2005?

The issue is well known since .NET 1.0. ASP.NET runtime is not able to identify that "</script>" is a string since runtime code does not parse script block content. Why? Because ASP.NET runtime supports pretty much every .NET-compliant language and it cannot assume particular syntax of the code in script blocks. Some languages may use single quotes, some may employ double quotes, some may do something else. The string may appear in comments and comment syntax is quite different is different languages. It is not possible to have a parser for every programming language in existence. Runtime has relatively simple parser that separates server-side elements from client-side elements and it identifies end of script blocks by doing plain text search for </script>. Simple parser also improves application performance. Actual script block content is eventually gets passed to the correspondent language compiler which knows how to deal with it.

VS 2005 simply matches runtime behavior. In VS 2003 the code seemed to work simply because VS 2003 did not support code intellisense or validation in server script blocks in ASPX files.  It, in fact, VS 2003 parsed C# script block content as javascript and VB script blocks as vbscript in order to provide basic code coloring. This actually confused users since VS 2003 showed that script block was valid at design time while at runtime ASP.NET would produce an error.

Comments

  • Anonymous
    November 09, 2005
    Which would probably be a good justification for using <asp.net:script>...</asp.net:script> as the server-side script delimiter, come to think of it (as opposed to <script runat=server>...</script>)
  • Anonymous
    November 09, 2005
    True. I believe runtime team simply decided to reuse classic ASP syntax.