C# 无法进行响应。当我使用Silverlight 4.0的WebClient调用aspx页面时重定向

C# 无法进行响应。当我使用Silverlight 4.0的WebClient调用aspx页面时重定向,c#,silverlight-4.0,webclient,response.redirect,server.transfer,C#,Silverlight 4.0,Webclient,Response.redirect,Server.transfer,我正在做一个Silverlight项目。 当我将jpg图片保存到memorystream以保存它时 在Context.InputStream中,它工作正常。 我正在调用一个aspx页面,该页面将上传线程连接到服务器 但当上传完成或失败时,我无法执行“response.redirect”或“server.transfer”。 是因为我使用WebClient从Silverlight调用我的aspx页面吗 请在下面的Silverlight中查找代码: private void UploadFile(

我正在做一个Silverlight项目。 当我将jpg图片保存到memorystream以保存它时 在Context.InputStream中,它工作正常。 我正在调用一个aspx页面,该页面将上传线程连接到服务器

但当上传完成或失败时,我无法执行“response.redirect”或“server.transfer”。 是因为我使用WebClient从Silverlight调用我的aspx页面吗

请在下面的Silverlight中查找代码:

 private void UploadFile(string fileName, Stream data){ 

 UriBuilder ub = new UriBuilder("http://localhost:52544/WebForm1.aspx");

//add a parameter filename  into the queryString

ub.Query = string.Format("filename={0}", fileName);

WebClient c = new WebClient();

c.OpenWriteCompleted += (sender, e) =>
   {

      PushData(data, e.Result);
      e.Result.Close();
      data.Close();
   };
c.OpenWriteAsync(ub.Uri);
}
在aspx页面上,我有以下代码

protected void Page_Load(object sender, EventArgs e)
        {
            try
            {             
                // get the filename

                string filename = Request.QueryString["filename"].ToString();

                // create a file on the server dir

                using (FileStream fs = File.Create(Server.MapPath("~/AppData/" + filename)))
                {
                    SaveFile(Request.InputStream, fs);
                }

                    Response.Redirect("uploadOk.aspx", true);
                }
                catch (Exception ex)
                {

                }


        }


        private bool SaveFile(Stream stream, FileStream fs)
        {
            bool isSaved = true;
            byte[] buffer = new byte[4096];
            int bytesRead;
            try
            {
                // copy the stream into the file

                while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
                {
                    fs.Write(buffer, 0, bytesRead);
                }
                isSaved = true;
            }
            catch (Exception e)
            {
                isSaved = false;
            }
            return isSaved;
        }
    }
我也尝试了response.redirection(“uploadOk.aspx”,false),但它不起作用。 我得到以下异常“[System.Threading.ThreadAbortException]={无法计算 表达式,因为代码已优化或本机帧位于调用堆栈顶部。}”

你知道如何使用WebClient进行重定向吗


提前感谢您

我相信您的问题在于上载失败,但由于它位于不同的线程上,SL未显示正确的错误。尝试添加代码来记录错误发生时的情况,并查看错误是什么。您也可以在服务器上执行此操作

我怀疑问题是标头已经写入,因此无法重定向

尝试此方法,因为我认为问题是100。请继续:

 c.Headers.Add( HttpRequestHeader.KeepAlive, "false");
 c.Headers.Add(HttpRequestHeader.Expect, "");

我相信你的问题是上传失败,但是因为它在不同的线程上,SL没有显示正确的错误。尝试添加代码来记录错误发生时的情况,并查看错误是什么。您也可以在服务器上执行此操作

我怀疑问题是标头已经写入,因此无法重定向

尝试此方法,因为我认为问题是100。请继续:

 c.Headers.Add( HttpRequestHeader.KeepAlive, "false");
 c.Headers.Add(HttpRequestHeader.Expect, "");

谢谢你的回答,但是我没有Silverlight中的方法c.Headers.Add。我只有所有的钥匙,可询问的,演员,。。。但不是Add方法。我认为你是对的,问题是头已经被写了。你能帮我修一下吗?谢谢你的回答,但是我没有Silverlight中的c.Headers.Add方法。我只有所有的钥匙,可询问的,演员,。。。但不是Add方法。我认为你是对的,问题是头已经被写了。你能帮我修一下吗?Thx提前。