0

I have converted an ASP.net mvc 2.0 web application to ASP.net MVC 4.6. it seems the same RedirectToAction code doesn't work in the new EF 4.6.

Basically, in the LogOn method of the AccountController, I have code:

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

to call the Index method of CalendarController. And within the CalendarController:Controller {, there is a Index() method such as:

[AcceptVerbs(HttpVerbs.Get]
public ActionResult Index() {..

These codes work in ASP.net MVC 2.0, the method Index() within RedirectToAction is called properly. But it is not working in asp.net MVC R4.6, in debug, RedirectToAction() just called the default constructor (public CalendarController()) of CalendarController, it doesn't execute the method Index() of CalendarController. Instead, after it executes the default constructor of CalendarController, it then executes Application_BeginRequest() method of Global.asax.cs, then it executes the Index() method of HomeController. The Index() method of CalendarController is ignored. From what I can see there is no Ajax post in the razor view, and the routing table is as below:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "ReferenceTables", // Route name
        "ReferenceTables/{action}/{tablename}/{id}", // URL with parameters
        new { controller = "ReferenceTables", action = "Index", id = @"" } // Parameter defaults
    );

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = "" } // Parameter defaults
   );
}

Does anybody know what caused the RedirectToAction is not working in my case? Thanks a lot for any help,

By the way, there is no Ajax post in the razor view

user2949042
  • 355
  • 2
  • 5
  • 15
  • FYI - there is no ASP.NET MVC 4.6, there is only ASP.NET MVC 2, 3, 4, and 5, all which can run on .NET Framework 4.6. – NightOwl888 Jan 29 '18 at 22:08
  • Are you customizing the startup in any way? Custom controller factory, action invoker, dependency resolver, etc? Please post the code from your `Global.asax.cs` and/or `Startup.cs`. – NightOwl888 Jan 29 '18 at 22:10
  • Sorry for the confusing..., yes, it was MVC EF 2 converted to .NET EF 4.6. I will post the Global.asax.cs and Startup.cs tomorrow. I also want to add that the codes of Index() of Calendar Controller contains the logic codes to verify user's ID and roles, because that codes was not executed, I think that is why it execute the method Index() of Home controller. I still need to figure out why the Index() of Calendar controller is not executed as RedirectToAction coded. Any ideas before I post the code tomorrow? thanks again – user2949042 Jan 29 '18 at 22:42
  • I just found this post, please see the response of Drew Kennedy , https://stackoverflow.com/questions/2225030/redirecttoaction-not-working it seems the [Authorize] caused that, I will see what is the impact if I remove [Authorize] from controller in this case. any idea about this? – user2949042 Jan 30 '18 at 01:11
  • somebody also mentioned this:return Redirect("/B/index"); //assuming your controller is called BController. it is worth to try, I will try it. – user2949042 Jan 30 '18 at 01:16
  • Of course, removing `[Authorize]` attribute means you are removing the security from the action method. It won't redirect to the login method like it is supposed to if you want it secured and the user isn't logged in. – NightOwl888 Jan 30 '18 at 02:26
  • But for this case, since it is an entranet web application, there is no external users, so all users who login are using their corporate Windows user ID, do you think I still need to keep [Authorize] there? – user2949042 Jan 30 '18 at 11:14
  • If this method is meant for everyone, that is fine. – NightOwl888 Jan 30 '18 at 11:56
  • Hi NightOwl888, thanks a lot. After checking the code, actually, I can not comment out the [Authorize role="Admin"], as I need this role to access the CalendarController(). After further debug, I found the problem is that after current user logged in, the current user role data as lost after the RedirectToAction call, so when the code execute to Calendar controller, it failed to execute because there is no Role data available. – user2949042 Jan 30 '18 at 21:13
  • But if I run it in Anonymous Authentication is Enabled, then the role data remains and program run correct. But in this case, I can not Enable the Anonymous authentication. Any idea to make the role data available between two controllers call? thanks – user2949042 Jan 30 '18 at 21:15
  • Great. I suggest posting your findings as an answer to this question, because it definitely answers why your `RedirectToAction` is not firing. As for your other question I suggest you first try to create a brand new MVC 5 project with authentication and then compare the differences in configuration with your current project. If you are still stumped and you can't find an answer by Googling, then ask a new StackOverflow question so you have the best chance of someone providing the right answer. – NightOwl888 Jan 30 '18 at 21:21
  • I will created another thread with a details info of my question. please check it and answer it if you have any idea from there. thanks – user2949042 Jan 30 '18 at 21:40

0 Answers0