Solved

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

Posted on 2011-09-13
10
5,207 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

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…
Online collaboration is quickly becoming embedded in the workplace, and its benefits are tangible. See what the current landscape looks like and what the future holds for collaboration tools and the future of work.
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

635 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