Reputation: 2032
I am using RedirectToRoute in my API controller to redirect to MVC controller. I was unable to find a good solution other than this:
return RedirectToRoute("Cv", new { action = "Cv/OpenCv" });
Previously I tried this below, but it didn't work.
return RedirectToRoute("Cv", new { controller="Cv", action = "OpenCv" });
Upvotes: 2
Views: 789
Reputation: 64
You can redirect from API controller to MVC controller by the help of below code. Please find below code for the above question.
public IHttpActionResult Index()
{
var newUrl = this.Url.Link("Default", new
{
Controller = "Account",
Action = "Register"
});
return Redirect(new Uri(newUrl.ToString(), UriKind.RelativeOrAbsolute));
}
or follow the direct call from API Controller
public IHttpActionResult Index()
{
return Redirect(new Uri("/Account/Register", UriKind.RelativeOrAbsolute));
}
Please use the above code and let me know
Upvotes: 1
Reputation: 443
Api and MVC Controllers use different Routes. RedirectToRoute in Api controller returns "System.Web.Http.Results.RedirectToRouteResult" but RedirectToRoute in Mvc Controller returns "System.Web.Mvc.RedirectToRouteResult". I think you can use just "Redirect" method in this case.
You can try to create an Mvc UrlHelper instance and use a RouteUrl method to build an Url from route parameters and then call Redirect method.
Upvotes: 3