Master page won't compile when referencing its own properties and members

拜拜、爱过 提交于 2020-01-07 02:10:05

问题


When upgrading a dotnet Web Site based on asp.net WebForms to a Web App I'm having trouble with Master pages.

My Master pages contain, in their .aspx file markup, references like this:

<div>
   Name: <% =GetUserName() %>  Account: <% =AccountId %>
</div>

GetUserName() is a protected method defined in my master page's codebehind file, and AccountId is a protected field in the same place.

After doing the Web App upgrade (which involves automatically generating a Site.Master.designer.cs codebehind file to accompany my own Site.Master.cs codebehind file) compilation of the Master page (not the codebehind) starts to fail, rejecting the protected member references in the <% =GetUserName() %> constructs. VS 2015 Intellisense says GetUserName not defined in this context.

But the Go To Definition... item on VS2015's context menu still works for the offending protected member references. Weird.

When I make a new Web App project from scratch, it doesn't have this problem. How can I fix this? Or is there something special about upgraded Web Site projects that prevents these protected member references?


回答1:


I figured this out.

The order of items matters in the project.csproj, and the conversion from Web Site to Web App doesn't always get it right.

To fix, close up VS2015, then use a text editor to open your project.csproj file. (It will be named for your project.) Look for the <Compile...> tags for your Master page's codebehind files.

Then make sure the tag to compile your own codebehind file comes before the one for the .designer.cs code, like this:

<Compile Include="Site.Master.cs">
  <DependentUpon>Site.Master</DependentUpon>
  <SubType>ASPXCodeBehind</SubType>
</Compile>
<Compile Include="Site.Master.designer.cs">
  <DependentUpon>Site.Master</DependentUpon>
</Compile>

In my experience, these tags were widely separated in the automatically generated .csproj file.

At any rate, moving one of them so they were in order resolved my problem.

Don't forget to Clean and Rebuild your project in VS2015 if you change your .csproj files outside the IDE.



来源:https://stackoverflow.com/questions/36202205/master-page-wont-compile-when-referencing-its-own-properties-and-members

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