简体   繁体   English

从同一服务器上的ASP.NET MVC5控制器调用经典ASP

[英]Calling Classic ASP from ASP.NET MVC5 Controller on the same server

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. 我正在将一个古老的经典asp项目迁移到ASP.NET,但是由于它是一件很大的老事,所以我会分块地进行。 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. 经典ASP和.NET的文件都在同一项目中。 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. 旧的经典asp项目使用Session变量(新的.NET项目不使用),因此我尝试调用经典的ASP页来执行此操作。

This is the classic asp "bridge": 这是经典的asp“桥梁”:

<!--#include virtual="/members/includes/db.config.asp" -->
<!--#include virtual="/members/includes/classes/clsLogin.asp" -->
<!--#include virtual="/members/includes/classes/clsAccess.asp" -->
<!--#include virtual="/members/includes/classes/clsUser.asp" -->
<!--#include virtual="/members/includes/classes/clsEntity.asp" -->
<!--#include virtual="/members/includes/classes/Database/clsDatabase.asp" -->
<!--#include virtual="/members/includes/classes/Helper/clsSessionVars.asp" -->
<!--#include virtual="/members/includes/classes/Helper/clsGlobal.asp" --><%'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. 但是,我遇到了真正的问题,无法从.NET Controller调用它并获取存储的会话变量。 I tried with a FileWebRequest: 我尝试了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: 我也尝试过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. 但是,我收到错误消息:无效的URI:无法确定URI的格式。

Can someone please offer me some guidance how I can get the .NET controller to call the classic ASP page. 有人可以为我提供一些指导,让我了解如何使.NET控制器调用经典的ASP页。

Thanks in advance, Rhys 在此先感谢,Rhys

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. 出现URI错误的原因是因为WebRequest不会发送到应用程序相对路径的请求,而是需要一个绝对路径。 It basically needs to reach URL's as a standard web browser would. 它基本上需要像标准Web浏览器那样到达URL。

Therefore, make sure your URL has a full path/host: 因此,请确保您的URL具有完整的路径/主机:

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. 我以localhost为例。 You would want to set the host to the same host as the calling script obviously. 您显然希望将主机设置为与调用脚本相同的主机。

NOTE: I'm still not sure this will entirely accomplish your objecting of capturing a Classic ASP session in .NET, but this should be a better start. NOTE:我仍然不确定这是否能完全完成捕获.NET中的Classic ASP会话的目标,但这应该是一个更好的开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM