Answered by:
Redirect to a page from Web API - APIController

Question
-
User6475278 posted
Hi..
I have developed the REST API using web API. Now i need to redirect to a page from httpppost method in api controller. What is the feasible solution to achieve this.
Need ur suggestions with examples..
Regards,
Gopal
Tuesday, September 16, 2014 1:38 AM
Answers
-
User-417640953 posted
Hi gopalnivas,
Thanks for your post.
You can try to return the page content to web api content like below:
[HttpPost] public HttpResponseMessage emp(emp jsondata) { if(jsondata.Name="") { //Here i need to redirect to a aspx page. var response = Request.CreateResponse(HttpStatusCode.Moved); response.Headers.Location = new Uri("your url...."); return response; } }
Similar issues you can follow below:
http://stackoverflow.com/questions/11324711/redirect-from-asp-net-web-api-post-action
http://stackoverflow.com/questions/20893670/webapi-redirect-not-working
Thanks.
Best Regards!
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, September 19, 2014 5:04 AM
All replies
-
User-760709272 posted
Have the webapi return the url to redirect to as its return data, or as a property on the data it returns, and your calling code will issue the redirect to that url.
Tuesday, September 16, 2014 3:41 AM -
User6475278 posted
Hi aidy,
Thanks for your response. We can able to redirect using web api return data via jquery etc. But i want to redirect to a page in web api httppost block itself. i.e) in apicontroller using c#.
How to do this. Need ur suggestions.
Regards,
Gopal
Tuesday, September 16, 2014 4:26 AM -
User-760709272 posted
You can't really do that, when you contact the webapi it is via a client object that communicates via http. To instigate a redirect the browser itself would need to post\get to the api directly. Just issue a redirect in the c# code, you don't need any javascript involved.
Tuesday, September 16, 2014 4:28 AM -
User6475278 posted
Hi Aidy,
Thanks for your reply.
Can you please share a sample code to achieve this.
Thanks,
Gopal
Tuesday, September 16, 2014 10:10 AM -
User-760709272 posted
string url = resultOfYourApiCall(); Response.Redirect(url);
Without knowing exactly what your api call does it's hard to give an example, just have the api return the url it wants the client to redirect to then redirect there.
Tuesday, September 16, 2014 10:40 AM -
User6475278 posted
Hi Aidy..
Thanks For you response.
Below is my sample code.
public class sampleApiController : ApiController
{[HttpPost]
public string emp(emp jsondata)
{if(jsondata.Name="")
{
//Here i need to redirect to a aspx page.
}
}
}
Waiting for ur suggestions..
Thanks,
Gopal
Thursday, September 18, 2014 4:25 AM -
User-417640953 posted
Hi gopalnivas,
Thanks for your post.
You can try to return the page content to web api content like below:
[HttpPost] public HttpResponseMessage emp(emp jsondata) { if(jsondata.Name="") { //Here i need to redirect to a aspx page. var response = Request.CreateResponse(HttpStatusCode.Moved); response.Headers.Location = new Uri("your url...."); return response; } }
Similar issues you can follow below:
http://stackoverflow.com/questions/11324711/redirect-from-asp-net-web-api-post-action
http://stackoverflow.com/questions/20893670/webapi-redirect-not-working
Thanks.
Best Regards!
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, September 19, 2014 5:04 AM