Hi I have a problem with my RedirectToAction not redirecting. My code successfully hits a breakpoint placed on the Redirect so I can confirm it is being called. Nothing seems to happen however.
I have tried looking at the network traffic in Chrome and there doesn't seem to be anything obvious. I must be missing something simple!
//
// POST: /Blog/CreateBlog
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateBlog(BlogViewModel model)
{
var userId = User.Identity.GetUserId();
model.UserId = userId;
if (ModelState.IsValid && model.UserId != null)
{
Mapper.CreateMap<BlogViewModel, Blog>();
if (_blogProcess.CreateBlog(Mapper.Map<BlogViewModel, Blog>(model)))
{
RedirectToAction("Index", "Blog");
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
RedirectToAction Result in MVC The RedirectToAction Result is returning the result to a specified controller and action method. Controller name is optional in RedirectToAction method. If not mentioned, Controller name redirects to a mentioned action method in the current Controller.
The RedirectToAction() Method. This method is used to redirect to specified action instead of rendering the HTML. In this case, the browser receives the redirect notification and make a new request for the specified action.
It is rendered to the page by URL. If we give the wrong URL, it will show 404-page errors. The RedirectToRouteResult is used whenever we need to go from one action method to another action method within the same or different controller in ASP.NET MVC Application.
Redirect Action Result in ASP.NET Core MVC. Step 1. Open Visual Studio 2019 and select the ASP.NET Core Web Application template and click Next. Step 2. Name the project FileResultActionsCoreMvc_Demo and click Create. Step 3. Select Web Application (Model-View-Controller), and then select Create. ...
try
return RedirectToAction("Index", "Blog");
In addtion to Nalaka526's answer: if we look into the documentation for RedirectToAction
we can see that it's an instance method of the Controller
class which has RedirectToRouteResult
as return type, which derives from ActionResult
which indicates that we have to return
it, just like we return View
and PartialView
for example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With