C# – Redirect to classic ASP failing with “Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack”

asp-classicasp.netc

I have the following code pasted below. For some reason, the response.redirect seems to be failing and it is maxing out the cpu on my server and just doesn't do anything. The .net code uploads the file fine, but does not redirect to the asp page to do the processing.

I know this is absolute rubbish why would you have .net code redirecting to classic asp, it is a legacy app. I have tried putting false or true etc. at the end of the redirect as I have read other people have had issues with this. It's so strange, it runs locally on my machine but won't run on my server. I am getting the following error when I debugged remotely:

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.

Update

After debugging remotely and taking the redirect out of the try catch, I have found that the redirect is trying to get to the correct location but after it leaves the redirect is just seems to get lost. (Almost as if it can't navigate away from the cobra_import project) back up a level to COBRA/pages. Why is this? This has worked previously!

public void btnUploadTheFile_Click(object Source, EventArgs evArgs) 
    { 
        //need to check that the uploaded file is an xls file.

        string strFileNameOnServer = "PJI3.txt"; 
        string strBaseLocation = ConfigurationSettings.AppSettings["str_file_location"]; 
        if ("" == strFileNameOnServer) 
        {
            txtOutput.InnerHtml = "Error - a file name must be specified."; 
            return; 
        } 
        if (null != uplTheFile.PostedFile) 
        { 
            try 
            { 
                uplTheFile.PostedFile.SaveAs(strBaseLocation+strFileNameOnServer); 
                txtOutput.InnerHtml = "File <b>" + strBaseLocation+strFileNameOnServer+"</b> uploaded successfully"; 

                Response.Redirect ("/COBRA/pages/sap_import_pji3_prc.asp");

            } 
            catch (Exception e) 
            { 
                txtOutput.InnerHtml = "Error saving <b>" + strBaseLocation+strFileNameOnServer+"</b><br>"+ e.ToString(); 
            } 
        } 
    } 

Best Answer

Response.Redirect doesn't work very smoothly inside a try...catch block. See this question.