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

穿精又带淫゛_ 提交于 2020-01-13 07:57:09

问题


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?


回答1:


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.




回答2:


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)




回答3:


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.




回答4:


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



回答5:


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

Have a look at the msdn compiler errors documentation:

  • http://msdn.microsoft.com/en-us/library/83ht1k63(v=vs.80).aspx

As in the example on MSDN:

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



回答6:


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



来源:https://stackoverflow.com/questions/9406915/asp-net-how-to-resolve-cs1513-expected-error-on-page

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