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
Related
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>
I am beginning developer in asp.Net MVC5.
In my MVC project i use with web service that return me a string URL the URL is from another domain.
I want to move to the URL.
For clear myself:
The client fill form home page and press submit, in the server side i send a request web
service with parameters from the form and get URL with another domain and this URL i need to present as second page to the client
public class HomeController : Controller
{
public ActionResult Home()
{
return View("~/Views/Home/home.cshtml");
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult doSomething(Something obj)
{
//use web service and get string URL
string urlString = ;// get from the web service response.
return View();// want write in the ();
}
}
Also this is usefull for navigation in MVC.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult doSomething(Something obj)
{
//use web service and get string URL
string urlString = ;// get from the web service response.
if (!string.IsNullOrEmpty(urlString))
{
//if the url is from within the domain.
return RedirectToAction(urlString);
//if the url is from other domain use this
//return Redirect(urlString);
}
//If the urlString is empty Return to a error page
return View("Error");
}
The url is from another site not the same domain
If you want to redirect to an external URL, you will need to use the Redirect() method.
Like so:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult DoSomething(Something obj)
{
// Use web service to get the string URL
string urlString = ...;
if (string.IsNullOrEmpty(urlString))
{
// If the urlString is empty, take the user to an Error View.
return View("Error");
}
// Redirect the user to the urlString
return Redirect(urlString);
}
I would recommend also doing some checking to ensure the URL is definitely valid. You can do this using the Uri static method, IsWellFormedUriString() - this returns a bool.
Like so:
if (!Uri.IsWellFormedUriString(urlString, UrlKind.Absolute))
{
// If the urlString is not a well-formed Uri, take the user to an Error View
return View("Error");
}
// Redirect the user to the urlString
return Redirect(urlString);
Alternatively, if you're redirecting to an internal Action, use the RedirectToAction() method, as #ankur suggested.
As an extra note: Ensure that your C# method names use PascalCase. Conserve camelCase for local variables/private fields.
So, you would use DoSomething(...), instead of doSomething(...) (I've done this in my example).
Hope this helps.
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");
}
I have two actions in a controller:
public ActionResult ReportRequest()
{
return View();
}
[HttpPost]
public ActionResult Report(ReportRequestObj rr, FormCollection formValues)
{
if (Request.HttpMethod != "POST")
return RedirectToAction("ReportRequest");
ReportingEngine re = new ReportingEngine();
Report report = re.GetReport(rr);
return View(report);
}
My problem is, the URL for the 'Report' page gets saved in the browser, and when I click on it, I get a 404, because the request has not been posted.
I put in a handler in to redirect to the report request page however on debugging it just doesn't seem to hit this at all.
Is there any other way I can determine if the request is a post, and if not redirect back to another page?
Thanks
Add action
public ActionResult Report()
{
return RedirectToAction("ReportRequest");
}
or just remove [HttpPost] from action Report
You cannot handle 404 error here because that request will never arrive to your action. You are using an action filter that makes sure that only POST requests arrive to that piece of code.
You should create another action method to respond to GET requests and return your view inside that.
[HttpGet]
public ActionResult Report()
{
return View("ReportRequest");
}
how to redirect from a action to an external web site like paypal
public ActionResult Index() {
return Redirect("http://www.paypal.com");
}