How can i redirect from a webmethod? - ASP.NET

I have an aspx page with a WebMethod to redirect the user to another page. The Default.aspx.cs is;

public partial class _Default : System.Web.UI.Page  
    public static void redirect_user()

and Default.aspx file;

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" ""> 
<html xmlns="">
<head runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">
        <script type="text/javascript">
        function Button1_onclick()  
            PageMethods.redirect_user(redirectSuccess, redirectError);
        function redirectSuccess(result)
        function redirectError(error)
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True" > 
            <input id="Button1" type="button" value="button" onclick="return Button1_onclick()" />

When i use HttpContext.Current.Response.Redirect("home.aspx") ; i have the "Thread was being aborted" error. And if i use the HttpContext.Current.Response.Redirect("home.aspx", false); overload i have the "Authentication failed" error. So how can i redirect from a webmethod?

Who is Participating?
gery128Connect With a Mentor Commented:
instead of redirecting user from pagemethods, use some return value of page methods to the callee function (the method which calls pagemethod) to redirect the user based on the return value (bool may be). Like you would get response from WebMethod true/false, and based on that you can always redirect user by checking that value in callee function.
Use response.redirect method
guveraAuthor Commented:
 Thanks for your reply. Please read my post carefully. I am getting  "Thread was being aborted" error.

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Response.RedirectLocation = "yourpage.aspx";
and if the above code not works, then you need to use javascript code
windows.location = "yourpage.aspx"
Kishan ZunjareSr. Software EngineerCommented:
I don't think this will work.
Redirecting is a browser / web related concept, while there are many Web service consumers that are not browser based.

A Webservice is meant to be a central smart layer that does a specific task. Let the webservice return a code to the calling client, that will tell whether it failed or succeeded or return any other information that will be required for the calling client to decide where to redirect the user to.

I think you'll need to rethink your strategy. For instance, have the calling code redirect the client. If necessary, have the Web service return the Ul the client needs to redirect to.

You can provide some sort of interface that you can nteract with rather than just a Page. I have never used Response.Redirect in a Web-Service.

And I think it is not the best way of doing whatever you want to do.
you are going in wrong direction here. He want to redirect from Page methods (EnablePageMethods="True") not from web service.
Easwaran ParamasivamCommented:
I don't think you could or should do that in a web service application.
A web service does not have a user interface anyway, so what's the pointof redirecting to another page?

Instead try to redirect from your ASPX page itself either in serveside using Response.Redirect. Or in client side using or window.OpenModelDialog.....
Rose BabuSenior Team ManagerCommented:

Make your webmethod redirect_user() with three output parameters like RedirURL, msgSuccess, msgFailure.

inside your redirect_user() method, do your process then return the values like

if true then
  RedirURL = <URL>
  msgSucc = <SUCCESS>
  msgFailure = <NULL OR EMPTY>
end if

then get these response in an object and parse the values. and then redirect the page to the url accordingly.

do your code as like the above and let me know.

Hope this may give you some idea to proceed. just try it once.
guveraAuthor Commented:
Good. Thanks for your help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.