I followed these instructions here: http://w3schools.com/razor/razor_example.asp NOTE: I'm using Web Matrix
The example said to do this:
@
{
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>
And all i get this this error:
Does anything have to be setup to accept @ { being on seperate lines?
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
If I put them on the same line, i get this error:
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: @开发者_JS百科{
Line 2: var imagePath;
Line 3: if( Request["Choice"] != null)
Source File: /Page.cshtml Line: 1
I don't know whats wrong.
Some people will stumble across this answer with just copying code samples from jquery.com into a razor file. For example I was testing something out and got the error from a long javascript variable value a regular expression specifically.
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])?)*$/,
Notice the @@ , I had to add the second @ to escape .... otherwise I get the error above and thus a google search shows me this stackoverflow page in the top result. So while this does not directly answer the question, I'm quite sure it could help someone else.
Here's how to fix the couple of errors in your code:
@{
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>
You need to remove line break after @ character.change you code like this
@{
var imagePath;
////other things..
here is a good syntax reference for you http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx and you need to change these lines too,
@{if(imagePath != "")
<img src="@imagePath" alt="Sample" />
}
Your code is wrong at var imagePath;
You can not declare with var
without assigning a value.
Just change it to:
var imagePath = "";
or
string imagePath;
精彩评论