Solved

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

Posted on 2011-09-13
10
4,749 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 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

22 Experts available now in Live!

Get 1:1 Help Now