Link to home
Start Free TrialLog in
Avatar of Tom McDonald
Tom McDonaldFlag for United States of America

asked on

migrate asp functions to asp.net

I have some asp pages with functions and I want to keep them as is during my migration to asp.net.  How do I do this?

All I really want to do is to change the page to an .aspx page (so I can use masterpages and get the page template I want).  However, it appears that I can't have an inline function in an aspx page.

For example, below I pasted some classic asp code into an aspx page

aspx page...
...<body>
<%

response.write(myFunction("Hi"))

function myFunction(value)
 myFunction = value & " there!"
end function
%>
</body> ...


When I try to run that in asp.net I get the error:

Compiler Error Message: BC30289: Statement cannot appear within a method body. End of method assumed.

Source Error:

Line 19: response.write(myFunction("Hi"))
Line 20:
Line 21: function myFunction(value)
Line 22:  myFunction = value & " there!"
Line 23: end function
 
I get the error on line 21
 
I don't want to migrate this to the code behind (royal pain) or into a control (even worse).  Ideas?

Avatar of Rusk
Rusk

use script runat=server to create functions
<script runat=server>
function myFunction(value as string) as string
    myFunction = value & " there!"
end function
</script>

alternatively you can make a class with a shared function to use from every page
or inherit from System.Web.UI.Page and create custom page class and inherit every form from the custom class.
This is an MS article on moving an asp app to asp.net
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/convertasptoaspnet.asp

also, do a lookup on google for the "aspcompatible" keyword.
it goes into the @page directive...
<%@ Page Language="vb" aspcompatable="true" ....%>
...not sure of all it does, but it might let asp things run better on an asp.net page
Avatar of Tom McDonald

ASKER

samtran0331, I got the following error when trying to use aspcompat=true:

Compiler Error Message: BC30289: Statement cannot appear within a method body. End of method assumed.

Source Error:

Line 19: response.write(myFunction("Hi"))
Line 20:
Line 21: function myFunction(value)
Line 22:  myFunction = value & " there!"
Line 23: end function
 
The error is on line 21

I read up on aspcompate and it does two things (neither of which helps me):

--ASP.NET uses Single-Threaded Apartment (STA) threads when accessing the COM component. The default is to use Multi-Threaded Apartment, or MTA, threading.
--ASP.NET provides access to the ASP-intrinsic objects in a backward-compatible fashion.

I tried it anyway and got the same error.
 


Rusk,

I tried the <script> tag and got a new error:

Compiler Error Message: BC30188: Declaration expected.

Source Error:

 

Line 17: <script language="vb" runat="server">
Line 18:
Line 19: response.write(myFunction("Hi"))
Line 20:
Line 21: function myFunction(value)
 


The error is on line 19 (instead of line 21), but I can't fix this error either. I suppose it wants me to add type declarations to the paramaters and function return value.  Ideas?
.Net will require you to declare vars

and your function is not returning anything:

Line 21: function myFunction(value)
Line 22:  myFunction = value & " there!"
Line 23: end function

Public Shared Function myFN(byval value as string) as string
myFN = value & " there!"
return myFN
End Function

This isn't .Net being picky....this is better coding syntax
Samtram, I added 'as string' to the end of the function, but I am still getting the same error:

Compiler Error Message: BC30188: Declaration expected.

Source Error:

Line 17: <script language="vb" runat="server">
Line 18:
Line 19: response.write(myFunction("Hi"))
Line 20:
Line 21: function myFunction(value) as string

Source File: http://localhost/dev/WebForm2.aspx    Line: 19

The error is still on line 19
Think about not doing the Classic ASP "spaghetti code" thing....like putting script tags in the middle of an html page.....
Try this:


<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<script language="vb" runat="server">
Public Shared Function myFN(byval value as string) as string
      myFN = value & " there!"
      return myFN
End Function

Sub Page_Load(Src As Object, E As EventArgs)
      If Not IsPostBack Then
            Try
            'Write to page
            Response.Write(myFN("Hello"))
            'Write to label
            lblText.Text = myFN("Hi")
            DataBind()
            Catch ex As Exception
            'Write the error to the label
                  lblText.Text = ex.Message.ToString
            End Try
      End If
End Sub
</script>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title></title>
</head>
<body>
<form runat="server">
<asp:Label ID="lblText" runat="server"></asp:Label>
</form>
</body>
</html>
Well Samtran, I hate to say it, but I think you're getting a little off track.  I am trying to convert asp to aspx with as little touching as possible.

From your last post it appears that you're telling me to completely convert my simple little 'hi there' classic asp function into asp.net complete with asp:label, code behind, etc.  

However, that is exactly what I am trying to avoid.  I would have to convert hundreds of pages and I don't want to go there.
...hmmm...I guess I am just a *little* off track... sorry dude....=0)

And I haven't messed with Master pages as that is a .Net 2.0 thing...but I have glanced at a little tutorial of it....and from what I saw...don't you have to touch every page you're going to apply the master page to? To remove the markup that would be rendered by the master page?

Like if your master page has <head></head> tags....and a child page has the <head></head> tags...if you apply the master to the child...would it get duplicated?
And then the child page has to know what region (defined in the master) to put the content in right?

So you're going to have to touch every single asp page any way it seems? And do a lot of work to get the .net master page thing to work...

Both FrontPage and Dreamweaver have templating abilities...that basically have the same end result as .net masterpages...this might be the easiest way to get a templating feature to your app..and just leave it as asp pages and not aspx.

...but a function like in your original post...

function myFunction(value)
 myFunction = value & " there!"
end function

will never ever work in .net....
syntax-wise...it has to return something and it has to know what you passed in

function myFunction(BYVAL value) AS STRING
 myFunction = value & " there!"
 RETURN myFunction
end function

sorry I couldn't be more help


Samtran, Yes, you're right, I will have to touch each page, but I want to minimize what I have to do on each page.  

Right now all I'll have to do is erase the header, left menu and footer includes and add 2 lines to the top of each page to implement the master page.  Pretty easy!

However, if I have to go through the code of every page and covert the whole darn thing to asp.net then I'm in for a LOT of work.

Anyway, I tried your suggestion of giving each function and parameter a type declaration.  This would be ok in my opinion, if that was all I had to do to upgrade the classic asp pages, but alas, even this didn't work.  The error is below:

Compiler Error Message: BC30188: Declaration expected.

Source Error:

 

Compiler Error Message: BC30188: Declaration expected.

Source Error:

 

Line 17: <script language="vb" runat="server">
Line 18:
Line 19: response.write(myFunction("Hi"))
Line 20:
Line 21: function myFunction(byval value as string) as string
 

Source File: http://localhost/dev/WebForm2.aspx    Line: 19
btw, I just tried adding 'public' before my function and got the same error.  I guess this is more difficult than I thought.  I'll up the ante a bit.
ASKER CERTIFIED SOLUTION
Avatar of samtran0331
samtran0331
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Samtran, I figured it out, sort of, i.e., I still have to add a page load event to the page to make it work.  

It turns out that the problem was in my page declaration.  I had all sorts of crap in there because I allowed visual studio to create the page automatically.  When I trimmed out all the crap like 'inherits=', 'autoeventwireup=' and 'codebehind=' then my simple little code started to work--even without type declarations on parameters and functions!

However, I still have to put the response.write within a 'page load' event.  Is there any way to avoid putting it within the page load event?

If there is no way to avoid it then I'll give the points to you because if there is no way to avoid using the page load event then that is a critical piece of the puzzle that you figured out.

Here is the (working) code as is:

<%@ Page Language="vb" %>
<script language="vb" runat="server">
Sub Page_Load(Src As Object, E As EventArgs)  
          Response.Write(myFN("Hello"))    
End Sub
Function myFN(value)
     myFN = value & " there!"    
End Function
</script>


yes...you didn't post the page directive so I didn't realize you were using the one added by vs.net...plus my sample coded has a stripped down @Page...

but dude...it's not "crap" =0)

the inherits part is how the page knows to use codebehind...

it's not an easy process...and I'm a fairly experienced asp dev...and moving to asp.net was pure hell for a little while...but once you get into it...you'll never want to touch asp again!
Oh, I agree.  It isn't 'crap'.  It is only crap in the sense that it was gumming up the migration process.  

I've been doing asp.net for about 3 years now, but all my sites have been from scratch.  This is the first time I've had to do a conversion/migration.  Migrations are absolute hell!  You might as well code it all from scratch.  MS dropped the ball on this one.
yeah...I agree...but asp and asp.net are so different in some respects that I don't think there could be an easy way to migrate....
This is a link to a migration "helper tool"...never used it tho...
http://www.asp.net/migrationassistants/asp2aspnet.aspx?tabindex=0&tabid=1
Oh, I disagree.  There could be an easy way to migrate.  All they would have to do is avoid what appears to be my problem:

1. don't require pages to change <% %> to <script></script>
2. don't require code to be 'on load' event

Basically, they should allow classic asp to run as is.  The changes required above are really just syntax and designation changes.  

MS easily could have avoided forcing the <script> tag.  What the sense forcing this change.  They could have allowed <% %> and just said that it will default to vb language type.  

They also could have avoided the 'on load' requirement by saying that, by default, all blocks inside <% %> tags are executed first and then the .net 'on load' runs (or vice versa).

Additionally, since 'response.write' is so prevelant they could have allowed backwards compatibilty for 'response.write' w/o parens.

Arggh, what were they thinking.
...hmmm...mebbe MS wanted to play Daddy Knows Best...not allowing asp to run "as is" forces you to comply to the new rules of .Net...
Yep, I think you put your finger on it.
Well, it appears that you are right.  Despite numerous attempts, it can't be done w/o using the page_load event.  As a result you can't execute "in the flow of the page" like you can in ASP, so that makes <asp:label> tags necessary.

btw, I was able to use include files if I converted the includes to vb and put in 'script' tags.  One thing that is useful was to migrate my include functions to the global.asax and then I could just instantiate a global object and call the function that way.  After I did that I saw that I couldn't really exe.

I wonder if I could have avoided this all by saying the page languange was vbscript.  Hmmm.  Maybe, but I'm not sure if my master pages would work then.  Oh well, topic for another post maybe.