Solved

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

Posted on 2011-09-13
10
4,802 Views
Last Modified: 2012-05-12
Hi;
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  
{
    [WebMethod]
    public static void redirect_user()
    {
        HttpContext.Current.Response.Redirect("home.aspx");
    }
}
 

and Default.aspx file;


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

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?

Thanks
Guvera
0
Comment
Question by:guvera
10 Comments
 

Expert Comment

by:mineshbihola
ID: 36534330
Use response.redirect method
0
 
LVL 8

Author Comment

by:guvera
ID: 36534574
Hi,
 
 Thanks for your reply. Please read my post carefully. I am getting  "Thread was being aborted" error.

Regards
Guvera
0
 
LVL 6

Expert Comment

by:badalpatel
ID: 36534578
Response.RedirectLocation = "yourpage.aspx";
0
 
LVL 6

Expert Comment

by:badalpatel
ID: 36534616
and if the above code not works, then you need to use javascript code
windows.location = "yourpage.aspx"
0
 
LVL 7

Expert Comment

by:Kishan Zunjare
ID: 36535092
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Expert Comment

by:badalpatel
ID: 36535195
Kishanzunjare:
you are going in wrong direction here. He want to redirect from Page methods (EnablePageMethods="True") not from web service.
0
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 36535332
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 window.open() or window.OpenModelDialog.....
0
 
LVL 16

Expert Comment

by:Rose Babu
ID: 36553183
Hi,

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.
0
 
LVL 9

Accepted Solution

by:
gery128 earned 500 total points
ID: 36902006
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.
0
 
LVL 8

Author Closing Comment

by:guvera
ID: 37044044
Good. Thanks for your help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now