Asked by:
Class/Module Scope on .aspx page - VB.NET

Question
-
User1843806588 posted
In playing with some of this stuff, I've moved a couple event functions from code-behind to inline.
Everything works fine in my IDE - but then the individual page breaks after being published to IIS.
So, I got the page working normally. Then I created the <script runat="server"></script> tags at the top of the ASPX page. I then cut the Page_Load event out of code-behind and put it in the script tags on the ASPX page with no modification.
This compiled and ran fine, so I then did the same with a button-click event. Everything ran fine. Then I published to an IIS server. When I click on the page, it then throws a BC30002 error with a class I created - "is not defined". The class is Public. No dice. It explodes before the page loads. Would I need to add it to a Namespace or something? Still works in my IDE.
I tried the same technique on another page and the page died with a Public Module with Error BC30451 - "is not declared. It may be inaccessible due to its protection level."
So, both of these look like it's some sort of scope issue. Is there a setting in Web.config or somewhere that I need to change? Again - only fails when deployed to IIS.
Friday, May 14, 2021 4:42 AM
All replies
-
User1535942433 posted
Hi Galaxy_Stranger,
How did you publish to iis? Have you checked whether there are related messages in the Event Viewer?
Best regards,
Yijing Sun
Friday, May 14, 2021 8:24 AM -
User753101303 posted
Hi,
Any reason for moving from code behind to inline scripting which is AFAIK a much less popular option? Where is defined the class you created?
You can use the @ Import directive to use the needed namespace or for a start, just try to type the full namespace in your code.
If you try to mix code behind and inline scripting, you may also have issues because of the compilation model (ie the code generated from the aspx page inherits from the code behind compiled code) and/or your deployment mode ie precompilation vs full compilation.
I would suggest:
- make 100% you REALLY need to use inline scripting. I would suggest to stick with code behind
- if you have to try first with the simpler test pageIt could be easier to find yourself the problem on a test page or you'll have the smallest amount of code needed to show the problem to others (it can discourage others to help if you show tons of code without having taken care of narrowding down at least a bit the problem).
Friday, May 14, 2021 8:36 AM -
User475983607 posted
I guessing you forgot to update the page directive. the page directive is still pointing to a partial class or file. It's very hard to provide accurate advice without the source code.
The follow reference documentation explains the differences between code-behind and in-line models.
https://docs.microsoft.com/en-us/troubleshoot/aspnet/code-behind-model
Friday, May 14, 2021 1:44 PM -
User-1545767719 posted
Can you provide a sample with minimum codes which reproduce your issue ?
Saturday, May 15, 2021 10:31 PM -
User1843806588 posted
Thanks for all the responses.
How did you publish to IIS?Right now, I'm publishing to a local directory, zip that up and then unzip it into the site directory on the server file system. In the past, we've published directly to the server file system via network share but that proved to be WAY too unreliable. So Visual Studio compiles it and then copies the files over to the production directory.Have you checked whether there are related messages in the Event Viewer?The Event Viewer just references the same errors I mentioned in my first post.Any reason for moving from code behind to inline...Because I am tasked with moving as much code to inline as possible. This is because we have to wait an entire day and bring the production site down after-hours to make minor changes, and then hope it doesn't take all night - or all weekend while I'm not being paid - to troubleshoot why the site didn't publish properly. The deficit in performance is considered a reasonable compromise for speed of development.This will limit publishing to changes in classes and modules, and page logic can be altered immediately.
Where is defined the class you created?All classes and modules are created in the App_Code directory. These are then compiled and not copied to the server.You can use the @ Import directive to use the needed namespace or for a start, just try to type the full namespace in your code.I tried this and also tried adding my class and module to a namespace. Just as before - this worked locally in Visual Studio, but not when published to the server.If you try to mix code behind and inline scripting, you may also have issues because of the compilation model (ie the code generated from the aspx page inherits from the code behind compiled code) and/or your deployment mode ie precompilation vs full compilation.Both of these issues are red flags to me. These are topics I'm also not too familiar with, and I'm not sure how to deal with them. I was able to change the publishing option "Precompile during publishing" check box, but that didn't make a difference. To be clear, all I am doing is taking an existing page that functions properly and moving a couple of existing code-behind events to Inline. Since it works seamlessly in Visual Studio, I'm convinced it's just a matter of making the application configuration happy on the server - possibly a compilation issue. I'll explain the current code layout and the changes I made below.I guessing you forgot to update the page directive. the page directive is still pointing to a partial class or file.I removed the reference to the code-behind file and removed the code behind file completely - and it didn't like that.Ok, the money-shot:
Public Class Login Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ' Login Page Load code goes here: End Sub Protected Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click ' Login code goes here: Dim objEventLog As New My_EventLog() ' the Class in question End sub End Class
Above is the code-behind file - Login.aspx.vb - originally created by Visual Studio when I added the page. I then CUT the subs out of code-behind and put them on the .aspx page which CURRENTLY looks like this:
<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" Inherits="MYPROJECT.Login" %> <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server"> <script Language="vb" runat="server"> Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ' Login Page Load code goes here: End Sub Protected Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click ' Login code goes here: Dim objEventLog As New My_EventLog() ' the Class in question End sub </script> <!-- HTML goes here: --> </asp:Content>
Tuesday, May 18, 2021 11:39 PM -
User1535942433 posted
Hi Galaxy_Stranger,
I think,you could do this:
1.Create a new project inside of your solution
2.Create this project as a c# class library
3.Compile that project either in debug or release (this will create a .dll in the projects debug/bin or release/bin folder).
4.Go back to your ASP.Net VB project and make a reference (right click the project in the explorer and your should see the reference option) to the DLL you just created.
Best regards,
Yijing Sun
Wednesday, May 19, 2021 9:43 AM -
User475983607 posted
Any reason for moving from code behind to inline...
Because I am tasked with moving as much code to inline as possible. This is because we have to wait an entire day and bring the production site down after-hours to make minor changes, and then hope it doesn't take all night - or all weekend while I'm not being paid - to troubleshoot why the site didn't publish properly. The deficit in performance is considered a reasonable compromise for speed of development.
Moving the code to inline does not change anything. Are you supporting a Web Site or Web Application project? The page directive below support a Web Application due the namespace with a dot. It does not make a lot sense to move the code inline for a web application project.
Above is the code-behind file - Login.aspx.vb - originally created by Visual Studio when I added the page. I then CUT the subs out of code-behind and put them on the .aspx page which CURRENTLY looks like this:
<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" Inherits="MYPROJECT.Login" %> <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server"> <script Language="vb" runat="server"> Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ' Login Page Load code goes here: End Sub Protected Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click ' Login code goes here: Dim objEventLog As New My_EventLog() ' the Class in question End sub </script> <!-- HTML goes here: --> </asp:Content>
As I suspected, the page directive is incorrect. The page directive inherits from MYPROJECT.Login which no longer exists because you removed the class or at least removed the class members so they can no longer be found. I think you'll be interested in taking the time to read the link in my first post to understand the two Web Forms page models; code behind and inline.
Can you clarify why you are moving the code to inline and what problem this solves?
Wednesday, May 19, 2021 10:48 AM