Having trouble "passing" this error from ASPNET_Compiler.exe:
error CS0103: The name 'TestMath' does not exist in the current context.
The function called within TestMath from a user control (.cs) file, is outside of the context (scope/namespace/...). If I comment out the line that calls a method within TestMath the compilation works and so does my user control (referenced the DLL from within a WebApplication).
The "TestMath"-class is a static class which contains one static method "Get", which returns -1+3 (I have also tried creating it non-static... without luck).
The normal build in Visual Studio (MSBuild) works, but as we all know; it does not pack .ascx files into the DLL properly.
I've attached a .rar containing:
- TestLibrary
Contains 1 .ascx control and 1 math class
Contains a post-build event, that runs the "BuildAssembly.cmd", which in turn runs aspnet_compiler and aspnet_merge.
- WebSite - one .aspx page, to display the control from the library.
The line that gives the error is located inside Label.ascx.cs, line 18:
Lbl.Text += " MATH: " + TestMath.Get()
Any suggestions at all? Take a guess? Calling a method in a class outside of the .ascx.cs is prohibited? Oh... But there's something to those words! Which mean; if I copy the Math-class inside the User-Control class, like so:
public class Label : UserControl
{
..methods of label
public class Math
{
public static Get()
}
}
The solution does compile and I am able to call methods within both classes... But, as we all want; I am no different: I want to also be able to call other objects (which I do already, they just live in GAC...)... so, hm... Basically; where does aspnet_compiler look for dll's...
Download example solution: http://s000.tinyupload.com/?file_id=76690332418132532036
If you un-pack the solution to C:\Solutions\Test, you can run it with little to no hassle, assuming Visual Studio 2010, Windows 7... .NET framwork 4 or higher.
Else, here's some code: 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 code behind
protected void Page_Load(object sender, EventArgs e)
{
Lbl.Text = "CodeBehindText...";
Lbl.Text += " MATH: " + TestMath.Get();
}
TestMath
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%
Wondering why all this "fuzz" with ASCX in 2015? Oh, hoped to reuse ascx files created several years ago, packing them in a library for eternity, signing the assembly, import it to my SharePoint-environment...