ASP.NET how to resolve CS1513: } expected error on page ASP.NET how to resolve CS1513: } expected error on page asp.net asp.net

ASP.NET how to resolve CS1513: } expected error on page


If the error code related as following:

A variable name same as reserved word then you can rename variable.

A code segment such as:

@model MyModel{    var appname = @Model.Apps.FirstOrDefault(x => x.ID == Model.SelectedApp);}

Remove '@' coming before Model.Apps.FirstOrDefault(x => x.ID == Model.SelectedApp)

A code segment or section usage such as:

@section{     <!-- hiiii it's not about an error -->}

Remove the apostrophe from comment in section.

If it is none of these specific cases you can attempt to locate where the error is generated by applying a source reduction. Delete/cut/comment out pieces of code until you can reliably turn the error off and on. The code that turns the error on is likely the culprit if it is not one of the above situations.


Look in the markup (aspx or ascx) for blocks like:

<% ... some C# code.... { %>   markup(controls, html etc)<% } %>

Any opened bracket { needs to be closed with another bracket }.

These pages or controls are compiled once by ASP .Net when they are first requested. Visual Studio doesn't compile aspx or ascx files.
If the project is "Web Site" type, Visual Studio compiles the aspx/ascx files, but if the project is "Web Application" type Visual Studio doesn't "compile" the markup (it does not generate the corresponding classes to the aspx/ascx markup)


On my site, the problem was caused by a block of code that looked like this:

            @{                  var currentNode = @linkedList.Find(@CurrentPage);                if (@currentNode.Next != null)                {                    var next = @currentNode.Next;                    <li>                        @next.Name                    </li>                }                if (@currentNode.Previous != null)                {                    var prev = @currentNode.Previous;                    <li>                        @prev.Name                    </li>                }            }

I'm not sure why the problem was caused by the nesting. This may be a bug in the compiler.