Solved

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

Posted on 2011-09-13
10
5,064 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Report: Liquid Web beats Amazon, Rackspace & More

A study by performance analyst firm Cloud Spectator finds that Liquid Web beats rivals Amazon, Rackspace and DigitalOcean when it comes to website and cloud application performance.

Question has a verified solution.

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

Suggested Solutions

For some reason Google Apps has decided to reserve 'www' for your new domain which results in an error when trying to map the web address 'www' to a certain site in your Google Sites. The error is 'this address is already being used' and after some …
To properly understand GitHub, let’s divide it into two words ‘Git’ and ‘Hub’. Git is basically a ‘Distribution Version Control’ (DVC) and ‘Source Code Management’ (SCM) system widely used by software programmers while Hub means the efficient centre…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

752 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