locked
Response.Redirect from web method in a service. RRS feed

  • Question

  • User1070938216 posted

    hello guys,

    i want to redirect from my webmethod to another domain, for example to https://www.google.com.

    i dont want to come back to aspx page to be redirected, all i want is to be redirected from my webmethod only.

    ........

    how i'll do that? , because httpcontext.currect.respose.redirect(myurl,false); is not working.

    please help me.

    thanx in advance.

    Tuesday, November 26, 2013 5:54 AM

All replies

  • User-484054684 posted

    AFAIK, we can't do Response.Redirect inside a webmethod.

    However,

    Option 1: You can do this on the client side after your webmethod is invoked.

    Reference: http://stackoverflow.com/questions/8322398/redirect-from-webmethod-in-asp-net

    Option 2: If you just want to invoke some url. You can make use of HttpWebRequest and HttpWebResponse classes or WebClient to make a HTTP request to other domain.

    Reference: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.getresponse(v=vs.110).aspx

    Tuesday, November 26, 2013 6:04 AM
  • User1070938216 posted

    AFAIK, we can't do Response.Redirect inside a webmethod.

    However,

    Option 1: You can do this on the client side after your webmethod is invoked.

    Reference: http://stackoverflow.com/questions/8322398/redirect-from-webmethod-in-asp-net

    Option 2: If you just want to invoke some url. You can make use of HttpWebRequest and HttpWebResponse classes or WebClient to make a HTTP request to other domain.

    Reference: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.getresponse(v=vs.110).aspx

    hi shiv,

    why we can't do Response.Redirect inside a webmethod? please explain me.

    i posted my conditon above in an image.

    and i wanted to be redirected from my webmethod, there may be something by which we can do this.

    Wednesday, November 27, 2013 8:57 AM
  • User-484054684 posted

    The purpose of WebMethods is to receive an input and process it and respond some output - Just like a C# method.

    That means, as people write methods but can't be consumed unless the code is shared - the webservices have been evolved.

    However, this is stateless most of the time, but a few ASP.NET features also can be included - example Session etc.

    Where as , Response.Redirect() will be used when we want to display some page to the user. This is a frond end operation and webservices/methods are meant for business logic.

    So would like to know, if you have tried the alternate approach of writing your logic on client side after you invoke the webmethod?

    Wednesday, November 27, 2013 9:05 AM
  • User1070938216 posted

    The purpose of WebMethods is to receive an input and process it and respond some output - Just like a C# method.

    That means, as people write methods but can't be consumed unless the code is shared - the webservices have been evolved.

    However, this is stateless most of the time, but a few ASP.NET features also can be included - example Session etc.

    Where as , Response.Redirect() will be used when we want to display some page to the user. This is a frond end operation and webservices/methods are meant for business logic.

    So would like to know, if you have tried the alternate approach of writing your logic on client side after you invoke the webmethod?

    M not asking about the concern that why method/function is made for....?

    I just want to know why its giving error, what is the framework/IIS restriction ?

    E.G. We can make class method in which we can redirect  user to some another page(URL), so why its not possible in service method having return type Void.

    Wednesday, November 27, 2013 9:29 AM
  • User-484054684 posted
    [WebMethod]
    public string HelloWorld()
    {
      Context.Response.StatusCode = 307;
      Context.Response.AddHeader("Location","<redirect URL>");
      return null;
    }

    Try it out and let me know if this is working for you. This should work, just replace the url in the place holder <redirect URL> in the above code to the url you would like to.

    Wednesday, November 27, 2013 10:02 AM
  • User-484054684 posted

    M not asking about the concern that why method/function is made for....?

    I just want to know why its giving error, what is the framework/IIS restriction ?

    E.G. We can make class method in which we can redirect  user to some another page(URL), so why its not possible in service method having return type Void.

    Also to answer the above question:

    Following is from MSDN:

    If you need to provide a redirect response in your Web service, do not use the Context.Response.Redirect method because the HTTP response will differ from what the Basic Profile mandates. [R1130]

    Wednesday, November 27, 2013 10:05 AM
  • User1070938216 posted

    thanx for your reply siva,

    but i tried your code too, 

    an error was thrown:

    The remote server returned an unexpected response: (405) Method Not Allowed.

    [WebException: The remote server returned an error: (405) Method Not Allowed.]
       System.Net.HttpWebRequest.GetResponse() +6440920
       System.ServiceModel.Channels.HttpChannelRequest.WaitForReply(TimeSpan timeout) +55
    
    [ProtocolException: The remote server returned an unexpected response: (405) Method Not Allowed.]
       System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) +10582079
       System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) +336
       BN.Web.Client.ServiceReference1.WebService1Soap.HelloWorld() +0
       BN.Web.Client.ServiceReference1.WebService1SoapClient.HelloWorld() in **:\PROJECTS\********\Service References\ServiceReference1\Reference.cs:53
       BloggerPlatform.Default.Page_Load(Object sender, EventArgs e) in **:\PROJECTS\********\Default.aspx.cs:16
       System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
       System.Web.UI.Control.OnLoad(EventArgs e) +92
       System.Web.UI.Control.LoadRecursive() +54
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772


    Thursday, November 28, 2013 2:07 AM