title: Calling Classic ASP from ASP.NET MVC5 Controller on the same server
body: <p>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.</p>
  
  <p>This is the classic asp "bridge":</p>
  
  <pre><code>&lt;!--#include virtual="/members/includes/db.config.asp" --&gt;
  &lt;!--#include virtual="/members/includes/classes/clsLogin.asp" --&gt;
  &lt;!--#include virtual="/members/includes/classes/clsAccess.asp" --&gt;
  &lt;!--#include virtual="/members/includes/classes/clsUser.asp" --&gt;
  &lt;!--#include virtual="/members/includes/classes/clsEntity.asp" --&gt;
  &lt;!--#include virtual="/members/includes/classes/Database/clsDatabase.asp" --&gt;
  &lt;!--#include virtual="/members/includes/classes/Helper/clsSessionVars.asp" --&gt;
  &lt;!--#include virtual="/members/includes/classes/Helper/clsGlobal.asp" --&gt;&lt;%'This file has global functions '%&gt;
  &lt;%
  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
  %&gt;
  </code></pre>
  
  <p>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: </p>
  
  <pre><code>private void SetClassicASPSessionVars(ApplicationUser user)
          {            
              try
              {
                  string url = "~/members/includes/classes/aspbridge.asp" + "?function=session&amp;userId=" + Url.Encode(user.UserId.ToString()) + "&amp;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);
              }
          }
  </code></pre>
  
  <p>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.</p>
  
  <p>I've also tried with HttpWebRequest:</p>
  
  <pre><code>try
              {
                  string url = "/members/includes/classes/aspbridge.asp" + "?function=session&amp;userId=" + Url.Encode(user.UserId.ToString()) + "&amp;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);
              }
  </code></pre>
  
  <p>However, I get the error:
  Invalid URI: The format of the URI could not be determined.</p>
  
  <p>Can someone please offer me some guidance how I can get the .NET controller to call the classic ASP page.</p>
  
  <p>Thanks in advance,
  Rhys</p>