Share via


Using Response.Redirect in a WebMethod

Question

Tuesday, June 5, 2007 10:19 PM

I  have a web service whhich contains a aweb method, the webmethod
generates email.

After generating an email in the web method I want to
refresh(Redirect) to another page.

I am currently doing this using this.context.Response.Redirect("http://
google.com"); this works when I invoke the web service directly from
an HTML page using the post method
<form runat="server" method="post" action="http://localhost:1205/
Websites/Service.asmx/Test
">
This works  fine and after the email is generated the page refreshes
to the right target.

The problem comes in when I invoke that same web method from an aspx
page via code behind.

In order to get this to work I add a web refernce to my aspx project
and then I call the web method progammatically.  In this case, my
refresh does not work even though the webservice is invoked.

Is there a reason why Response .Redirect doesnot work when I invoke my
web service programmatically.

However, it works when I invoke it directly via a HTML page.

All replies (4)

Wednesday, June 6, 2007 11:33 PM âś…Answered

HI, ishvinder:
You can try this syntax: 
[WebMethod]
public string HelloWorld()
{
  Context.Response.StatusCode = 307;
  Context.Response.AddHeader("Location","<redirect URL>");
  return null;
}

http://msdn.microsoft.com/en-us/library/ms953974.aspx

I hope the above information will be helpful. If you have any issues or concerns, please let me know. It's my pleasure to be of assistance


Wednesday, June 6, 2007 1:31 AM

HI..

Could you try using the server.transfer (http://google.com);

It is all done at the server so will work even if you have already sent some output to the client

or else you could try the Response.Redirect("http://google.com",false); 

Thanks.


Wednesday, June 6, 2007 9:24 AM

Hi,

Thanks Venkatzeus

 I have tried both Server.transfer(http://google.com);

Server.Exceute(http://google.com);

and also Response.redirect(http://google.com,false);

But nothing is working....

Also, the exact error which I am getting is...

The request failed with the error message: -- <html><head><title>Object moved</title></head><body> <h4>Object moved to <a href=http://www.google.com>here</a>.</h4> </body></html> <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><OrderEmailwithParametersResponse xmlns="http://exmplar.com/" /></soap:Body></soap:Envelope> --.


Wednesday, June 6, 2007 9:38 AM

Hi..

Could you please check : http://support.microsoft.com/kb/888418

Could you try to refer the web service again and then running the application?

Could you please also try doing:

response.clear();  and then using response.redirect()

Thanks