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

6

12

I am getting an error at run time when viewing my ASP.NET page in the browser. I am not getting any build errors however I am getting the following compiler error at runtime:

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1513: } expected

Source Error:


Line 329:            #line hidden
Line 330:            __output.Write("\r\n\t\t\t</div>\r\n\t\t");
Line 331:        }
Line 332:        
Line 333:        private System.Web.UI.Control __BuildControl__control7() {

Source File: c:\Windows\Microsoft.NET\Framework\v1.1.4322\
    Temporary ASP.NET Files\xxxxxxxx\450ffa78\d46d847d\
    k1gsz9dj.0.cs    Line: 331 

I cannot locate any missing } in my source code and this error is occurring in the generated code files that exist in the Temporary ASP.NET Files directory. How can I trace this to the line of code that is actually malformed in my page or user controls on my page?

Kassa answered 23/2, 2012 at 3:52 Comment(8)
Have you tried looking for that .cs file in the folder specified?Bridgettbridgette
It's on my server, i don't have access to the server.Kassa
it clearly says "}" is missing check on those lines ...Mesoblast
I have different user controls called into an aspx page. I can't simply find the line. that is why I am asking that line 333 is part of which user control? how can i tell?Kassa
Seriously the path given by the debugger is the file you need to openBridgettbridgette
That is the file which .NET generates on the fly, so stop suggesting that it says which line you need to find, because it doesn't exist in the source code.Antonietta
[Edit: See answer post below]Darksome
Page on Prod site wasn't loading. Dev also failed with an exception that included the file, eg > SourceCode: "#pragma checksum > \"D:\ProjPath\\Views\\Employee\\Details.cshtml\" Cleaned, rebooted, etc to no avail. Commented out the page, ran it successfully, then uncommented section by section until... the whole page ran ok. Schrodinger's Bug, it disappears when you look at it. The punchline? It was then ok on the production server. And no, the data didn't change. All I can come up with is that it had to do with a local browser file or something.Alten
M
16

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.

Masked answered 13/11, 2013 at 9:5 Comment(1)
Encountered this today, my situation was #2 where I had @Model. in a code segment. It seems like as of Razor 2 that the last example with the HTML comment and apostrophe was fixed by the razor generator. I could not reproduce that being an error.Luggage
R
3

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)

Robena answered 23/2, 2012 at 5:42 Comment(1)
It compiles aspx in my experience, but I believe you're right about this being in an ascxBridgettbridgette
D
0

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.

Darksome answered 29/8, 2014 at 8:3 Comment(1)
I believe the error in your case is var next = @currentNode.Next; and var prev = @currentNode.Previous; both of those are inside of code segments. I believe the razor generator would want var next = currentNode.Next; note the lack of the @ in this line.Luggage
G
0

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
Geniculate answered 25/10, 2016 at 14:48 Comment(0)
B
-1

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()
      {
      }
   }
Bottommost answered 23/2, 2012 at 4:39 Comment(1)
but it shouldn't let the thing compile at that point, to get to the point of showing the files in the temporary_files folderBridgettbridgette
T
-1

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

Tips answered 23/2, 2012 at 5:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.