Solved

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

Posted on 2011-09-13
10
4,921 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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
 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 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