当我按照这个例子时,为什么会出现Parser Error?

Wil*_*ins 2 razor asp.net-mvc-3

我在这里按照这些说明操作:http://w3schools.com/razor/razor_example.asp 注意:我正在使用Web Matrix

这个例子说这样做:

@
{
var imagePath; 
if( Request["Choice"] != null)
{imagePath="images\" + Request["Choice"];} 
} 
<!DOCTYPE html> 
<html> 
<body> 
<h1>Display Images</h1> 
<form method="post" action=""> 
<div>
   I want to see: 
   <select name="Choice"> 
      <option value="Photo1.jpg">Photo 1</option> 
      <option value="Photo2.jpg">Photo 2</option> 
      <option value="Photo3.jpg">Photo 3</option> 
   </select> 
   &nbsp; 
   <input type="submit" value="Submit" /> 
</div> 
<div style="padding:10px;"> 
@if(imagePath != "")
{<img src="@imagePath" alt="Sample" />} 
</div> 
</form> 
</body> 
</html> 
Run Code Online (Sandbox Code Playgroud)

所有我得到这个错误:

有什么必须设置接受@ {在单独的行?

    Server Error in '/' Application.
    Parser Error 
    Description: An error occurred during the parsing of a resource
    required to service this request. Please review the following 
    specific parse error details and modify your source file appropriately. 

    Parser Error Message: A space or line break was encountered after
    the "@" character.  Only valid identifiers, keywords, comments, 
    "(" and "{" are valid at the start of a code block and they must 
    occur immediately following "@" with no space in between.


    Source Error: 


    Line 1:  @
    Line 2:  {
    Line 3:  var imagePath; 


    Source File: /Page.cshtml    Line: 1 


    Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1 
Run Code Online (Sandbox Code Playgroud)

如果我把它们放在同一行,我会收到此错误:

    Parser Error 
    Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately. 

    Parser Error Message: The code block is missing a closing "}" character.  Make sure you have a matching "}" character for all the "{" characters within this block, and that none of the "}" characters are being interpreted as markup.


    Source Error: 


    Line 1:  @{
    Line 2:  var imagePath; 
    Line 3:  if( Request["Choice"] != null)


    Source File: /Page.cshtml    Line: 1
Run Code Online (Sandbox Code Playgroud)

我不知道什么是错的.

小智 5

只需将jquery.com中的代码示例复制到剃刀文件中,有些人就会偶然发现这个问题.例如,我正在测试一些东西,并从一个长的javascript变量值得到一个正则表达式的错误.

 emailRegex = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,
Run Code Online (Sandbox Code Playgroud)

注意@@,我必须添加第二个@来逃避....否则我得到上面的错误,因此谷歌搜索显示我在顶部结果中的这个stackoverflow页面.所以虽然这并没有直接回答这个问题,但我确信它可以帮助别人.