我在这里按照这些说明操作: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>
<input type="submit" value="Submit" />
</div>
<div style="padding:10px;">
@if(imagePath != "")
{<img src="@imagePath" alt="Sample" />}
</div>
</form>
</body>
</html>
所有我得到这个错误:
是否必须设置接受@ {在单独的行上?
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
如果我把它们放在同一行,我会收到这个错误:
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
我不知道什么是错的。
答案 0 :(得分: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])?)*$/,
注意@@,我必须添加第二个@来逃避....否则我得到上面的错误,因此谷歌搜索显示我在顶部结果中的这个stackoverflow页面。所以虽然这不能直接回答这个问题,但我确信它可以帮助别人。
答案 1 :(得分:2)
以下是修复代码中的几个错误的方法:
@{
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>
<input type="submit" value="Submit" />
</div>
<div style="padding:10px;">
@if(imagePath != "") {
<img src="@imagePath" alt="Sample" />
}
</div>
</form>
</body>
</html>
答案 2 :(得分:1)
您需要在@ character.change之后删除换行符,就像这样
@{
var imagePath;
////other things..
这是一个很好的语法参考http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx 你也需要改变这些线,
@{if(imagePath != "")
<img src="@imagePath" alt="Sample" />
}
答案 3 :(得分:1)
var imagePath;
的代码错误
如果不指定值,则无法使用var
声明
只需将其更改为:
var imagePath = "";
或
string imagePath;