forum

Home / DeveloperSection / Forums / How to call classic ASP from ASP.NET MVC5 controller?

How to call classic ASP from ASP.NET MVC5 controller?

Mark M212403-Nov-2014

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


Updated on 03-Nov-2014

Can you answer this question?


Answer

1 Answers

Liked By