Redirect to another website from Controller - c#

how to redirect from a action to an external web site like paypal

public ActionResult Index() {
return Redirect("http://www.paypal.com");
}

Related

ASP.NET Core API - View is not displayed after redirect from Api Controller

I am having some troubles while redirecting from Api Controller to Controller. I can see in postman response, that correct View is returned.
public class ApiController : Controller
{
[HttpPost("/someApiAction/{id}")]
public async Task<IActionResult> SomeApiAction(string id)
{
return RedirectToAction("Action", "Other", new { id = id});
}
}
Other Controller:
public class OtherController : Controller
{
public async Task<IActionResult> Action(string id)
{
var model = new Model();
return View(model);
}
}
Code reaches return View(model) but it's not displayed.
Can anybody please help on this one?
I assume you are requesting the web api controller from PostMan instead of Html page. For check the page from Postman, you need to copy the content out and view it from browser by view html online site.
For Postman, it will not be able to open web browser from the response.
Try to check the html by request the api controller like code below:
<form asp-controller="UserApi" asp-action="SomeApiAction" asp-route-id="2" formmethod="post">
<button type="submit">Request Api</button>
</form>

Redirect to ActionResult from another website

I have MVC project that use with web service to get a url and then do a redirect to the URL that i get.
In the first request(the request that return me the URL in the response) i need provider some details and one of them is successUrl.
successUrl is URL that if the action of the client in the URL that i was redirect before success so redirect to successUrl.
My question is: it possible to redirect to ActionResult? if yes What i need to send in successUrl?
Update
For Example:
I have my MVC Project and Web Service.
My controller-
public class HomeController : Controller
{
public ActionResult Home()
{
return View("~/Views/Home/home.cshtml");
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CheckObject(someObject obj)
{
if (ModelState.IsValid)
{
ResponseWS responseWs = wsRequest(deatils);
}
}
[HttpPost]
public ActionResult doSomething()
{
//....do something...
}
}
The steps-
First the client go to home page from ActionResult Home().
The client fill form and click submit and go to
ActionResult CheckObject(someObject obj)
From ActionResult CheckObject(someObject obj) send request to WS
with parameters that one of them urlSuccees.
Ws return response with URL(for the example www.test.com) that i
need to redirect to it.
I do the redirect to www.test.com.
The client do something and if the client action was success the
www.test.com need to redirect to my urlSuccees.
NOTE: I want that urlSuccees will be ActionResult doSomething()
Thanks,
Tal

Redirect from APIController action to other MVC controller action

I created a web api controller that has an register action:
public class MyProjApiController : ApiController
{
public IHttpActionResult Register()
{
return RedirectToAction("Register", "AccountController"); //??
}
}
And I am trying to redirect to another action in MVC Controller public class AccountController : Controller that has public async Task<ActionResult> Register(RegVM model) but my Register action in MyProjApiController has IHttpActionResult return type and register in AccountController has Task<ActionResult> type - how to call it?
I think you are mixing two concepts here. Your API controller should have endpoints which will be called from some client and you should be returning some data back in a format which client can read, XML or JSON. You should not be doing a redirect to another MVC controller.
What you should be doing is return some data which has the path to the MVC action controller which the client can use.
public HttpResponseMessage Register()
{
//DO something as you want
var newUrl = this.Url.Link("Default", new { Controller = "Account",
Action = "Register" });
return Request.CreateResponse(HttpStatusCode.OK,
new {Success = true, RedirectUrl = newUrl});
}
This will return you a response like this to the caller with 200 OK response status code
{
"Success" : true,
"RedirectUrl" : "yoursite.com/Account/Register"
}
The client should read this and do the necessary things. For example, If you are calling this API from your js code, You can simply use window.location.href to redirect the user to the new page.
For example ,
$.post("PathToYourApiEndpoint",function(res){
if(res.Success)
{
window.location.href = res.RedirectUrl;
}
});
Again,I am not sure why you are calling the API first and the redirecting the user to the MVC controller action. Whatever logic you are doing in the Web api action method, you might be able to do in your MVC controller and thus avoid your API call.
If you mean from the API help file. I wanted to disable except for local.
if (!Request.IsLocal)
{
Response.Redirect("/home");
}

oauth redirecting in asp.net mvc4

I will try to implement redirect with oauth authorization in asp.net mvc4 project
controller
public ActionResult SomeName() {
if (!User.Identity.IsAuthenticated) {
return RedirectToAction("ExternalLogin", "Account", new { provider = "vkontakte" });
}
}
account
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult ExternalLogin(string provider) {
return new ExternalLoginResult(provider, Url.Action("ExternalLoginCallback"));
}
error
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its
dependencies) could have been removed, had its name changed, or is temporarily
unavailable.
Please review the following URL and make sure that it is spelled correctly.
Requested URL: /Account/ExternalLogin
Does anybody know what I should to do?
try this:
[HttpGet]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult ExternalLogin(string provider) {
return new ExternalLoginResult(provider, Url.Action("ExternalLoginCallback"));
}
By using RedirectToAction you are making a GET request to the url of your action, you need to accept HttpGet at ExternalLogin action on your AccountController
RedirectToAction is 302 redirect request which is GET by nature. if you are supposed to utilize your action from VIEW as well, you can use both verbs :
[HttpGet, HttpPost]
at ExternalLogin action on your AccountController accept HttpGet as other member also said.

Call controller from another controller and return a view

I call an Action from a Login controller to authenticate users, once the user is authenticated I would like to call either the Cashier or the Supervisor action, depending on the user's role, and display the appropriate view.
I can break on AuthenticateUserByCard but RedirectToAction doesn't seem to be working.
I'm not sure if what I'm trying to do is deviating from the MVC architecture, if so please suggest the correct way to do this
Login controller:
public class LoginController : Controller
{
public ViewResult Index()
{
return View();
}
[HttpPost]
public ActionResult AuthenticateUserByCard(string token)
{
//Authenticate user and redirect to a specific view based on the user role
Role role = GetRoleByToken(token);
if(role.UserType == UserType.Supervisor)
return RedirectToAction("Supervisor", "Login", new { id = token });
else
return RedirectToAction("Cashier", "Login", new { id = token });
return null;
}
public ActionResult Supervisor(string id)
{
//Do some processing and display the Supervisor View
return View();
}
public ActionResult Cashier(string id)
{
//Do some processing and display the Cashier View
return View();
}
}
Java Script:
$.get("/Login/AuthenticateUserByCard",{token:token});
jQuery post and get ignore 301 browser redirects returned from the server. You would normally need to handle them yourself. This can get messy: How to manage a redirect request after a jQuery Ajax call
All you really need in this case is to return the choice of methods, but make them return explicit views (not implicit). The default would always be to return the view based on the IIS-called method i.e. "AuthenticateUserByCard" unless you specify the view.
e.g.
public class LoginController : Controller
{
public ViewResult Index()
{
return View();
}
[HttpPost]
public ActionResult AuthenticateUserByCard(string token)
{
//Authenticate user and redirect to a specific view based on the user role
Role role = GetRoleByToken(token);
if(role.UserType == UserType.Supervisor)
return Supervisor(token);
else
return Cashier(token);
return null;
}
public ActionResult Supervisor(string id)
{
//Do some processing and display the Supervisor View
return View("Supervisor");
}
public ActionResult Cashier(string id)
{
//Do some processing and display the Cashier View
return View("Cashier");
}
This will not change the URL though. If you need that too try the other answer I linked. You basically handle the redirect in jQuery and goto the new page.
Alternatively, to change the URL, put the desired URL into a hidden field of the returned views and extract that value to update the browser URL (just a thought) :)

Categories

Resources