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

匆匆过客 提交于 2019-12-05 01:45:04
Suresh Mahawar

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.

I got a similar problem and oculd find it only after a log of trial and error.

The error I made was to add a '@' to variables inside a foreach loop which started with:

@foreach

Well as the error suggests, you are missing a closing curly brace '}'

Have a look at the msdn compiler errors documentation:

As in the example on MSDN:

// the below will cause CS1513 since namespace is missing '}'
namespace y    
{
   class x
   {
      public static void Main()
      {
      }
   }

try to compile it in visual studio. i think it will also show where the exact line of the code that has incomplete curly braces.

compilation error cs1513

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!