views:

654

answers:

4

Hey everyone,

I'm developing a new ASP.NET MVC 2.0 application and wanting to use the new ASP.NET 4 encoding blocks.

My View code contains <%: Model.ActivityName %> however Visual Studio is reporting:

Unexpected token

at the position of the : (colon). When I run the application I get the following compilation error:

Compiler Error Message: CS1525: Invalid expression term ':'

What am i missing?

Cheers for any help/advice.

+6  A: 

This feature is new to ASP.Net 4.0, which is in turn new to Visual Studio 2010.

In earlier versions, you should write

<%= Html.Encode(Model.ActivityName) %>
SLaks
Thanks Schabse. I'd completely omited the idea that I might perhaps need to dev this in VS 2010. Much appreciated.
Jamie Dixon
You don't need to use VS2010 to use ASP.Net MVC.
SLaks
Yeah I know. I just want to be able to use the codeblocks feature. This is a brand new project so I want to use the most uptodate things I can. My MVC 1 projects have all been dev'd in 2008
Jamie Dixon
A: 

are you running ASP.NET 4?

Glennular
+1  A: 

as has already been pointed out, you need to use the correct version of VS. if you can't and you need to change all of your code to use the Html.Encode method Phil Haack had a post about doing the opposite which might be useful.

Not that I have tied it, but the reverse search and replace in VS should be something like

\<\%:b*{[^%]*}:b*\%\>

replaced with

<%:= Html.Encode\( \1 \) %>

or something close to that. Like I say I've no IDE at the moment to test, so try it before you run it over your entire project.

Sam Holder
Cheers Sam and thanks for the blog link. I read this just yesterday infact. :)
Jamie Dixon
+4  A: 

Sounds like the target framework is not set to .NET Framework 4.0.

To do this, in the solution explorer, right click the project root and select properties in the context menu.

In the application tab you should see a drop down for Target Framework. Change this to .NET Framework 4.0.

Try and compile to see if the error goes away.

DevDave
Bingo! That did it for me. I'd been working on another project in 3.5 and that stuck when I started the new solution. Thanks DevDave!
kdmurray