从不扩展System.Web.UI.Page的类中获取System.Web.HttpResponse

本文关键字:Web System HttpResponse 获取 UI 扩展 Page | 更新日期: 2023-09-27 18:29:43

虽然标题说明了一切,但我想让您知道我需要它的特定场景。

在应用程序中,我想通过调用JavaScript方法注销用户。此方法依次调用WebMethod:

namespace EMSApplication.Web.WebServices {
    /// <summary>
    /// Holds the Webservice methods of EMSApplication
    /// </summary>
    [WebService(Namespace = "http://ems-app.com/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class EMSWebService : System.Web.Services.WebService {   
        [WebMethod]
        public string Logout() {
        if (User.Identity.IsAuthenticated) {
            System.Diagnostics.Debug.WriteLine(" ==JYM00000000000000000000000000000");
            FormsAuthentication.SignOut();
            Response.Redirect("~/default.aspx");
        }
        return "";
        }
    }
}

我该如何解决这个问题?

从不扩展System.Web.UI.Page的类中获取System.Web.HttpResponse

响应.重定向将创建一个302 Http响应,这对浏览器来说意味着它需要将用户重定向到新位置。

由于现在调用是由javascript进行的,因此javascript需要解释响应并指示浏览器加载新位置。

我现在想的最简单的方法是Logout()方法返回新位置,JavaScript将设置window.location属性

window.location = msg.d;

从WebService中,您可以访问来自HttpContext类(HttpContext.Current.Response.Redirect())的响应,但您不需要它。实际上,我不认为Web服务应该编写其他响应,而不是通常的响应(SOAP等)。

HttpContext.Current.Response

这是一个静态属性。