Re: Question on - Automatic compilation of VB.NET files on a Web Server

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Microsoft News (spakh_at_us-installations.com)
Date: 03/23/05


Date: Wed, 23 Mar 2005 09:27:07 -0500

Ok..another stumbling block ... grrrrrr

The <%@ Assembly ... %> page directive only works if the class name is
referenced on that same .aspx page.
Trying to call a method in MySecurity.vb from Default.aspx.vb will not work.
Is there a way to make this work without having to compile MySecurity.vb
into an assembly?
Here's the source I wrote using WebMatrix editor for a quick test that
produces an error.

Files:
C:/Projects/Sandbox/Default.aspx
C:/Projects/Sandbox/Default.aspx.vb
C:/Projects/Sandbox/Components/BLL/MySecurity.vb

The following line in Default.aspx.vb breaks:
---> Dim Result As MySecurity = New Sandbox.MySecurity

I would really like to be able to make this scenario work. Is it possible?

Thank you again :D

SOURCE CODE

<---Start Default.aspx page
<%@ Page Language="VB" Src="Default.aspx.vb" Inherits="_Default" %>
<%@ Import Namespace="Sandbox.BLL" %>
<%@ Assembly Src="Components/BLL/MySecurity.vb" %>

<html>
<head>
</head>
<body>
    <form runat="server">
        <p>
            <asp:Label id="Label1" runat="server">Label</asp:Label>
        </p>
        <p>
            <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
        </p>
        <p>
            <asp:Calendar id="Calendar1" runat="server" BackColor="White"
Width="200px" DayNameFormat="FirstLetter" ForeColor="Black" Height="180px"
Font-Size="8pt" Font-Names="Verdana" BorderColor="#999999" CellPadding="4">
                <TodayDayStyle forecolor="Black"
backcolor="#CCCCCC"></TodayDayStyle>
                <SelectorStyle backcolor="#CCCCCC"></SelectorStyle>
                <NextPrevStyle verticalalign="Bottom"></NextPrevStyle>
                <DayHeaderStyle font-size="7pt" font-bold="True"
backcolor="#CCCCCC"></DayHeaderStyle>
                <SelectedDayStyle font-bold="True" forecolor="White"
backcolor="#666666"></SelectedDayStyle>
                <TitleStyle font-bold="True" bordercolor="Black"
backcolor="#999999"></TitleStyle>
                <WeekendDayStyle backcolor="#FFFFCC"></WeekendDayStyle>
                <OtherMonthDayStyle
forecolor="#808080"></OtherMonthDayStyle>
            </asp:Calendar>
        </p>
        <p>
            <asp:Button id="Button1" runat="server"
Text="Button"></asp:Button>
        </p>
    </form>
</body>
</html>
<---End Default.aspx page

<---Start Default.aspx.vb
Imports System

Public Class _Default
    Inherits System.Web.UI.Page

    Protected Label1 As System.Web.UI.WebControls.Label
    Protected TextBox1 As System.Web.UI.WebControls.TextBox
    Protected Calendar1 As System.Web.UI.WebControls.Calendar
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button

 Public Sub New()
 End Sub

 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
  If Not IsPostback() Then
   Label1.Text = "YooHoo"
  End If
 End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
Button1.Click
     Dim Result As MySecurity = New Sandbox.MySecurity

        Label1.Text = "Hello, " & TextBox1.Text & "! Welcome on " &
Calendar1.SelectedDate & "." & "You have been - " & Result.Authenticate(0)
    End Sub

End Class
<---End Default.aspx.vb

<---Start MySecurity.vb
Imports System

Namespace Sandbox.BLL
 Public Class MySecurity
  Public Sub New()
  End Sub

     Public Function Authenticate(ByVal UserID As Int32) As String
        Return "Authenticated"
     End Function
 End Class
End Namespace
<---End MySecurity.vb

"Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
news:157606632470823721491456@msnews.microsoft.com...
> You can add an <%@ Assembly Src="YourOtherFile.vb" %> to the page to have
> ASP.NET compile it. Otherwise ASP.NET doesn't know about it. In ASP.NET
2.0
> there's a special App_Code directory specifically for this purpose.
>
> -Brock
> DevelopMentor
> http://staff.develop.com/ballen
>
>
>
> > Ok, a follow up question, plz.
> >
> > We now have .vb component files that do not have any corresponding
> > .aspx pages to them. Would they also go through the JIT compilation
> > when a method is called on them?
> >
> > ie.
> >
> > /Logon.aspx
> > /Logon.aspx.vb --> calls Security.Authenticate(UserID)
> > /Components/BAL/Security.vb --> calls SecurityDB.Authenticate(UserID)
> > /Components/DAL/SecurityDB.vb --> Retreives data from a DB using
> > Stored
> > Proc.
> > Now, will SecurityDB.vb and Security.vb also get compiled and executed
> > like Logon.aspx.vb would with the Src attribute in Logon.aspx?
> >
> > Thanks again.
> >
> > "Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
> > news:143371632470278966670352@msnews.microsoft.com...
> >
> >> Yes, ASP.NET does this for you. VS.NET on the other hand doesn't like
> >> this model. VS.NET prefers that you precompile all of the codebehind
> >> files (.vb files) prior to deploying tot he webserver. So you end up
> >> deploying ASPX files and DLL files (no .VB files). But it's ok, you
> >> can alter this
> >>
> > behavior
> >
> >> if you want to.
> >>
> >> The trick to chaning this behavior is to change the CodeBehind
> >> attribute in the ASPX to be Src. Src asks ASP.NET to compiled the
> >> referenced .vb
> >>
> > file.
> >
> >> This is an unfortunate PITA of working with VS.NET -- it wants to do
> >>
> > things
> >
> >> its way. Now if you're comfortable with compiling all the .vb files
> >> into a DLL and deploying that instead of live .vb files, then you'll
> >> get along better with VS.NET.
> >>
> >> It's also possible to not use VS.NET at all. The other compelling
> >> tool is WebMatrix[1] and it's much more in the style of deployment
> >> you've
> >>
> > described,
> >
> >> meaning it doesn't compile the codebehind files. The major drawback
> >> with WebMartix is that there is no intellisense support. HTH
> >>
> >> [1] http://www.asp.net/webmatrix/default.aspx?tabIndex=4&tabId=46
> >>
> >> -Brock
> >> DevelopMentor
> >> http://staff.develop.com/ballen
> >>> Not sure if this is the correct newsgroup. Could be the IIS.
> >>> Hopefully someone can answer my question.
> >>>
> >>> We are migration an ASP Web App to VB.NET. One of the benefits we're
> >>> enjoying so far with ASP is that we do not have to worry about a
> >>> build release. When an ASP file is modified and tested, we just
> >>> deploy that file to the production Web server through Visual
> >>> SourceSafe without worrying that we are releasing someone elses
> >>> untested/unfinished code out.
> >>>
> >>> Now I am hoping that .NET provides the same scenario where all we
> >>> need to do is copy the .aspx and .vb files to the Web Server, and
> >>> all the .vb files would automatically get compiled into the assembly
> >>> when an .aspx file is requested. Meaning that we wouldn't have to
> >>> compile the application into a DLL assembly. The environment would
> >>> recognize that the .vb file has changed and would get compiled into
> >>> the existing assembly.
> >>>
> >>> My question, does .NET work this way?
> >>>
> >>> Thank you.
> >>>
>
>
>


Quantcast