I will need to redirect a URL from my Web Method such that when my application calls my web method, it will redirect to the URL contained in my Web Service.
I tried using System.Web.HttpContext.Current.Response.Redirect("url"); However, it compiled properly but doesn't work in my application, instead, it gave me the following error:
Description:
An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Net.WebException: The request failed with the error message:
--
<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="http://www.microsoft.com">here</a>.</h2>
</body></html>
--.
Any other alternatives for it? I really need to redirect an url from my Web Service.
Thanks for your help, however, the webRequest & webResponse method doesn't seem to work. Do I need to consider other things (separate class file?) before I insert those methods or just simply dump it into my Web Service? Anyway, my url(s) are all external
links.
Your client currently calls your web service at some particular URL. You want your web service to redirect all calls so that they go to a different URL, is that correct?
Assuming that the "object moved" exception you posted came from the client, then you need to configure your client to permit automatic redirection. You'll set the
AllowAutoRedirect property of the proxy class. See
Ways to Customize your ASMX Client Proxy for details.
I inserted the 'AllowAutoRedirect = true' in my application, a new error came out:
Why is it requesting my web application to be in 'XML' mode and not 'HTML'??
Server Error in '/IndividualLightingTest' Application.
Client found response content type of 'text/html; charset=utf-8', but expected 'text/xml'.
The request failed with the error message:
--
You're calling a web service, right? It uses XML. So your client expects XML. For some reason, there's an error on the server and so you get an error message back in HTML.
You'll have to use Fiddler or something to watch the network traffic and see what the HTML is about. It may be an error message.
In fact, you should look at the event log on the server to see if the error is maybe already logged.
I tested the Web Service (.asmx w/o any application) via the Browser(http://localhost/ZZZ.asmx), I clicked on the web method that contained the System.Web.HttpContext.Current.Response.Redirect(http://www.microsoft.com);
the Web Service can invoke the URL w/o any problems. However, if I use the Web Application to actually call the above method, error occurs. What could be the possible reason?
I suspected it is the problem from my Web Application but I really have no idea what exactly it is.
Assuming that the "object moved" exception you posted came from the client, then you need to configure your client to permit automatic redirection. You'll set the
AllowAutoRedirect property of the proxy class. See
Ways to Customize your ASMX Client Proxy for details.
tecksheng
Member
2 Points
9 Posts
URL Redirect from Web Method
Jul 10, 2009 02:09 AM|LINK
I will need to redirect a URL from my Web Method such that when my application calls my web method, it will redirect to the URL contained in my Web Service.
I tried using System.Web.HttpContext.Current.Response.Redirect("url"); However, it compiled properly but doesn't work in my application, instead, it gave me the following error:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Net.WebException: The request failed with the error message:
--
<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="http://www.microsoft.com">here</a>.</h2>
</body></html>
--.
Any other alternatives for it? I really need to redirect an url from my Web Service.
qwe123kids
All-Star
48619 Points
7957 Posts
MVP
Re: URL Redirect from Web Method
Jul 10, 2009 04:59 AM|LINK
Hi,
donot redirect the page.. Rather Use WebRequest,webresponse Method To Hit Server internally
http://www.c-sharpcorner.com/UploadFile/mahesh/WebRequestNResponseMDB12012005232323PM/WebRequestNResponseMDB.aspx
http://msdn.microsoft.com/en-us/library/456dfw4f.aspx
Avinash Tiwari
Remember to click “Mark as Answer” on the post, if it helps you.
tecksheng
Member
2 Points
9 Posts
Re: URL Redirect from Web Method
Jul 13, 2009 01:45 AM|LINK
Hi qwek123kids
Thanks for your help, however, the webRequest & webResponse method doesn't seem to work. Do I need to consider other things (separate class file?) before I insert those methods or just simply dump it into my Web Service? Anyway, my url(s) are all external links.
johnwsaunder...
Star
11262 Points
1981 Posts
Re: URL Redirect from Web Method
Jul 13, 2009 03:38 AM|LINK
Let me make sure I understand.
Your client currently calls your web service at some particular URL. You want your web service to redirect all calls so that they go to a different URL, is that correct?
Assuming that the "object moved" exception you posted came from the client, then you need to configure your client to permit automatic redirection. You'll set the AllowAutoRedirect property of the proxy class. See Ways to Customize your ASMX Client Proxy for details.
tecksheng
Member
2 Points
9 Posts
Re: URL Redirect from Web Method
Jul 14, 2009 01:43 AM|LINK
Hi johnwsaunders3
I inserted the 'AllowAutoRedirect = true' in my application, a new error came out:
Why is it requesting my web application to be in 'XML' mode and not 'HTML'??
Server Error in '/IndividualLightingTest' Application.
Client found response content type of 'text/html; charset=utf-8', but expected 'text/xml'.
The request failed with the error message:
--
johnwsaunder...
Star
11262 Points
1981 Posts
Re: URL Redirect from Web Method
Jul 14, 2009 02:17 AM|LINK
You're calling a web service, right? It uses XML. So your client expects XML. For some reason, there's an error on the server and so you get an error message back in HTML.
You'll have to use Fiddler or something to watch the network traffic and see what the HTML is about. It may be an error message.
In fact, you should look at the event log on the server to see if the error is maybe already logged.
tecksheng
Member
2 Points
9 Posts
Re: URL Redirect from Web Method
Jul 14, 2009 04:24 AM|LINK
I tested the Web Service (.asmx w/o any application) via the Browser(http://localhost/ZZZ.asmx), I clicked on the web method that contained the System.Web.HttpContext.Current.Response.Redirect(http://www.microsoft.com); the Web Service can invoke the URL w/o any problems. However, if I use the Web Application to actually call the above method, error occurs. What could be the possible reason?
I suspected it is the problem from my Web Application but I really have no idea what exactly it is.
johnwsaunder...
Star
11262 Points
1981 Posts
Re: URL Redirect from Web Method
Jul 14, 2009 04:45 AM|LINK
As I said before:
Assuming that the "object moved" exception you posted came from the client, then you need to configure your client to permit automatic redirection. You'll set the AllowAutoRedirect property of the proxy class. See Ways to Customize your ASMX Client Proxy for details.
qwe123kids
All-Star
48619 Points
7957 Posts
MVP
Re: URL Redirect from Web Method
Jul 14, 2009 05:00 AM|LINK
Hi,
http://mikehadlow.blogspot.com/2006/05/making-raw-web-service-calls-with.html
Chk the above Link it conatins sample Of server to server hit on Webservice using csharp
Avinash Tiwari
Remember to click “Mark as Answer” on the post, if it helps you.
deesh1531982
Contributor
4766 Points
823 Posts
Re: URL Redirect from Web Method
Jul 14, 2009 11:58 AM|LINK
try this
Context.Response.Clear()
Context.Response.Buffer = False
Context.Response.Redirect("yoururl")