从 ASPNET_Compiler.exe “传递”此错误时遇到问题:
错误 CS0103:当前上下文中不存在名称“TestMath”。
从用户控件 (.cs) 文件在 TestMath 中调用的函数位于上下文之外(范围/命名空间/...)。如果我注释掉调用 TestMath 中的方法的行,则编译工作,我的用户控件也是如此(从 WebApplication 中引用 DLL)。
“TestMath”类是一个静态类,它包含一个静态方法“Get”,它返回-1+3(我也尝试过创建它非静态的......没有运气)。
Visual Studio (MSBuild) 中的正常构建工作,但众所周知;它不会正确地将 .ascx 文件打包到 DLL 中。
我附上了一个.rar,其中包含:
- 测试库
包含 1 个 .ascx 控件和 1 个数学类
包含一个运行“BuildAssembly.cmd”的构建后事件,该事件又运行 aspnet_compiler 和 aspnet_merge。
- 网站 - 一个 .aspx 页面,用于显示库中的控件。
给出错误的行位于 Label.ascx.cs 的第 18 行:
Lbl.Text += " MATH: " + TestMath.Get()
有什么建议吗?猜一下?禁止在 .ascx.cs 之外的类中调用方法?哦……但是这些话是有道理的!意思是说; 如果我在 User-Control 类中复制数学类,如下所示:
public class Label : UserControl
{
..methods of label
public class Math
{
public static Get()
}
}
该解决方案确实可以编译,我可以在两个类中调用方法......但是,正如我们所愿;我也不例外:我也希望能够调用其他对象(我已经这样做了,它们只是生活在 GAC 中......)......所以,嗯......基本上;aspnet_compiler 在哪里寻找 dll 的...
下载示例解决方案: http ://s000.tinyupload.com/?file_id=76690332418132532036
如果您将解决方案解压缩到 C:\Solutions\Test,则可以毫不费力地运行它,前提是 Visual Studio 2010、Windows 7....NET 框架 4 或更高版本。
否则,这里有一些代码:ASCX
<%@ Control Language="C#"
AutoEventWireup="true"
ClassName="TestLibrary.Controls.Label"
CodeFile="Label.ascx.cs"
CodeBehind="Label.ascx.cs"
Inherits="TestLibrary.Controls.LabelCode" %>
Test
<asp:Label runat="server" ID="Lbl"></asp:Label>
背后的 ASCX 代码
protected void Page_Load(object sender, EventArgs e)
{
Lbl.Text = "CodeBehindText...";
Lbl.Text += " MATH: " + TestMath.Get();
}
测试数学
public static int Get()
{
return -1 + 3;
}
BuildAssembly.cmd
SET compiler_path=%systemroot%\Microsoft.NET\Framework64\v4.0.30319
SET aspnet_merge=C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\aspnet_merge.exe
SET websiteproject=C:\Solutions\Test\TestLibrary\TestLibrary
SET compiled=C:\Solutions\Test\TestLibrary\TestLibrary\Compiled
SET outputfolder=C:\Solutions\Test\TestLibrary\TestLibrary\bin\Release
SET outputfilenamedll=TestLibrary.dll
%compiler_path%\aspnet_compiler.exe -p "%websiteproject%" -c -f -d -v / %compiled%
"%aspnet_merge%" %compiled% -o %outputfilenamedll%
COPY /Y %compiled%\bin\%outputfilenamedll% %outputfolder%\%outputfilenamedll%
RMDIR /s /q %compiled%
想知道为什么 2015 年 ASCX 出现了这么多“模糊”?哦,希望重用几年前创建的 ascx 文件,将它们打包在一个库中以备不时之需,签署程序集,将其导入我的 SharePoint 环境......