0

I am working Asp.Net Core. I need to call the action method from one controller to another.

I tried by using the RedirectToAction method. When I am using this, I get an error like "No route matched the supplied values".

enter image description here

Code I used:

return RedirectToAction("Logon", "Login");

We are using the route files like this

enter image description here

I am calling from MyItemsController to LoginController. The route of "Logon" is present in LoginRouteUrl file.

If I add the route in MyItemsRouteURL file means, the RedirectToAction is working. i.e) the method "Logon" in Login controller is executed. But I need to add the route for Logon in LoginRouteURL.

And I need to redirect the Action Result.

How to achieve this?

Tony Ngo
  • 19,166
  • 4
  • 38
  • 60
  • see this https://stackoverflow.com/questions/16870413/how-to-call-another-controller-action-from-a-controller-in-mvc – Amin Saadati Jun 26 '19 at 05:31
  • If I use like this means, ```var result = new ControllerB().FileUploadMsgView("some string");``` , I am not able to redirect this result. I need to redirect the action result. – Nivitha Gopalakrishnan Jun 26 '19 at 05:49

1 Answers1

1

You should use RedirectToAction

return RedirectToAction("Index", "Home");

First params will be your action name, second param will be your controller name

Also make sure you have default route setting like this

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller}/{action=Index}/{id?}");
        });
Tony Ngo
  • 19,166
  • 4
  • 38
  • 60