We can create Asp .Net classes or bussiness objects inside App_Code or using compiled library .dll file in Bin folder.
Alternatively classes may be write inside WebService. If nothing is wrong then we can access our web app classes when it is launched via Visual Studio.
Unfortunately, after we deploy it to IIS and try to access deployed classes or WebService an error “Compilation error .. Type xxx is not defined ..” occurs.
Let’s make a New Website Asp .Net Empty Web Application to make problem and solution clear.
- Create a new Website with location as File System
- Add new item -> WebForm -> Default.aspx. Write one label control inside form tag:
<form id="form1" runat="server"> <div> <asp:Label ID="msg" runat="server"></asp:Label> </div> </form>
- Add New Item -> Class -> Hello.vb -> Place it to App_code folder. Hello class have one function only to return ‘Hello World’:
Imports Microsoft.VisualBasic Public Class Hello Public Sub New() End Sub Public Function SayHello() As String Return "Hello World" End Function End Class
- At Code behind Default.aspx write inside Page_load event:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim hlo As New Hello() msg.Text = hlo.SayHello() End Sub
So when page is load the msg label control text will be set to ‘Hello World’
- Right click Default.aspx from solution explorer and then view in browser. The page contains Hello World text without error. So, when this website launched via Visual Studio no error occured.
So if I access the web app with url: http://localhost/aspapp/aspnetcode/Default.aspx then it should be ok but I was wrong. A ‘Compilation error. Type ‘Hello is not defined’ occur
The solution is quite simple, make this ‘AspNetCode’ web app folder as a virtual directory.
- Open IIS and create new virtual directory and names alias as ‘AspNetCode’
- Browse to ‘AspNetCode’ folder and then make sure you choose ‘write’ and ‘execute’ checkbox, finish
Try again with url: http://localhost/AspNetCode/Default.aspx (without aspapp part because aspnetcode is virtual dir now) then it is ok now. 🙂
- Click Connect
- Choose Local IIS or FTP or Remote and then click create new web application icon name it ‘AspNetCode’
- Block all source code on left pane and copy them to the right pane
Alternative 2, Create a new website with web location of HTTP
- Create a new website application
- Choose Web Location to HTTP and write http://localhost/AspNetCode/ on its textbox, do not choose file system
- Write all source code from start
Alternative 3, Create a new project and choose asp .net empty website. Write necessary source codes and publish the project to IIS to make sure we create/upload to a virtual directory
Bin folder is created when we reference library (.dll) file. This .dll file is pre-compiled class. Problem and solution are same as above.
Same problem with same solution.
- Create a new website choose Web Location to HTTP and fill textbox with http://localhost/UsingWebService
- Add new item -> WebForm -> Default.aspx. Add one msg label same as above Default.aspx
- Add new item -> Web Service -> Hello.asmx. Leave code as it is
- Right click website app at solution explorer, click add web reference, click Web Services in this Solution, choose Hello.
- Write Web Reference Name to HelloWs
- Edit code behind file of Default.aspx:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim hlo As New HelloWs.Hello() msg.Text = hlo.HelloWorld() End Sub
Same as above, this website intend to write ‘Hello world’ at Page load.
- Run this website with right click Default.aspx file, click view in browser or through IIS with url: http://localhost/UsingWebService/Default.aspx. It should be no error
To know more about Asp .Net Webservices just click this url: Quickly Mastering: Create And Use Asp .Net Web Service From Basic, Database Access To Ajax-Enabled
Solution to the problem Compilation Error when accesing app_code / bin folder or WebService is making a virtual directory or website refer to your website application root folder. The classes under app_code / bin folder must on root of virtual directory.