Home > DeveloperSection > Forums > How to call classic ASP from ASP.NET MVC5 controller?
Kamlakar Singh
Kamlakar Singh

Total Post:28

Points:196
Posted on    November-03-2014 1:20 AM

 ASP.Net C#  ASP.NET MVC  Http Request 
Ratings:


 1 Reply(s)
 705  View(s)
Rate this:

Hi Guys

I am migrating an old classic asp project over to ASP.NET, but as it is such a big old thing I am doing it in pieces. So I am at a stage where I need them to work together. The files for both classic asp and .NET are all in the same project. The old classic asp project uses Session variables (the new .NET project doesn't), so I am attempting to call a classic ASP page to do that.

This is the classic asp "bridge":

<%'This file has global functions '%>

<%

Dim userId : userId = CStr(Request.QueryString("userId"))

Dim username : username = Cstr(Request.QueryString("username"))

Dim objAccess : Set objAccess = new clsAccess

Dim objDb : Set objDb =  new clsDatabase

Dim objUser : Set objUser = new clsUser

Dim objLogin : Set objLogin = new clsLogin

Dim objEntity : Set objEntity = new clsEntity

'Set user session etc.

if(Request.QueryString("function") = "session") then    

    Dim userRecord : Set userRecord = objUser.GetUserById(userId) 

    Dim rsEntity : Set rsEntity = objEntity.GetById(userRecord("EntityId"))

    'Login classic asp : Sets the session variables.            

    Call objLogin.LoginUser(userRecord, username, rsEntity) 

'Kill the session i.e. Log out

elseif(Request.QueryString("function") = "kill") then

    Session.Contents.RemoveAll()

    Session.Abandon()   

    Response.Write("Session Killed")

End if

%>

However, I am having real problems being able to call this from the .NET Controller and get the session variables stored. I tried with a FileWebRequest:

private void SetClassicASPSessionVars(ApplicationUser user)

        {

            try

            {

                string url = "~/members/includes/classes/aspbridge.asp" + "?function=session&userId=" + Url.Encode(user.UserId.ToString()) + "&username=" + Url.Encode(user.UserName);

                Uri serverUri = new Uri(url);

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serverUri);

                request.Method = "POST";

 

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())

                {

                    StreamReader reader = new StreamReader(response.GetResponseStream());

                    var content = reader.ReadToEnd();

                }

            }

 

            catch (WebException we)

            {

                throw new InvalidOperationException("Error logging in user to classic asp system. User: " + user.UserId + ". Exception: " + we);

            }

        }

This only read the file and didn't execute the script, so it only returned the source code and didn't create the session variables.

I've also tried with HttpWebRequest:

try

            {

                string url = "/members/includes/classes/aspbridge.asp" + "?function=session&userId=" + Url.Encode(user.UserId.ToString()) + "&username=" + Url.Encode(user.UserName);

                Uri serverUri = new Uri(url, UriKind.Absolute);

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serverUri);

                request.Method = "POST";

 

                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())

                {

                    StreamReader reader = new StreamReader(response.GetResponseStream());

                    var content = reader.ReadToEnd();

                }

            }

 

            catch (WebException we)

            {

                throw new InvalidOperationException("Error logging in user to classic asp system. User: " + user.UserId + ". Exception: " + we);

            }

However, I get the error: Invalid URI: The format of the URI could not be determined.

Can someone please offer me some guidance how I can get the .NET controller to call the classic ASP page.

Thanks



Barbara Jones

Total Post:13

Points:91
Posted on    November-03-2014 3:50 AM

The reason you're getting the URI error is because WebRequest does not send to requests to relative paths to your application, instead it needs an absolute path. It basically needs to reach URL's as a standard web browser would.

Therefore, make sure your URL has a full path/host:

string url = "http://localhost/members/includes/classes/aspbridge.asp" + "?function=session&userId=" + Url.Encode(user.UserId.ToString()) + "&username=" + Url.Encode(user.UserName);


I used localhost here as an example. You would want to set the host to the same host as the calling script obviously.


Don't want to miss updates? Please click the below button!

Follow MindStick