简体   繁体   中英

Get System.Web.HttpResponse from a class which does not extend System.Web.UI.Page

Though the title says everything, I want to let you know the particular scenario where I need this.

In application I want to logout an user by calling a JavaScript method. This method in turns is calling a 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 "";
        }
    }
}

How can I solve this?

HttpContext.Current.Response

这是一个静态属性。

The Response.Redirect will create a 302 Http Response which for the browser means it will need to redirect the user to that new location.

Since now the call is made from javascript, the JavaScript needs to interpret the response and to instruct the browser to load the new location.

The easiest way I'm thinking now is the Logout() method to return the new location and JavaScript will set the window.location property

window.location = msg.d;

From an WebService you can access the Response from HttpContext class ( HttpContext.Current.Response.Redirect() ), but you won't need that. Actually I don't think that a webservice should write another response than the usual ones (SOAP, etc).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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