0
votes

I have the following route table:

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

        routes.MapRoute(
            name: "UserRoute",
            url: "{username}",
            defaults: new { controller = "User", action = "Index" }
        );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

so, when we have url like : http://mysite/abcde it calls UserController, action Index, if we have url like : http://mysite/dashboard/Index it calls DashboardController, action Index. Ok. But when I try to call the following:

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

It calls UserController, action Index with username parameter equals "Dashboard". Why and how to solve?

1
You UserRoute matches anything with only one segment (which your RedirectToAction() generates because the default action is Index)user3559349
@StephenMuecke is right. What are you trying to achieve with your userRoute?. By default all controllers redirect to the index action if no action is passed... correct?David Espino
It sounds to me like you need to create a custom route constraint that only matches the {username} section of your first route if, and only if a user exists of that name. Even then, you'd still be in trouble if a user decided to call themselves Home... The core of this problem is having such a greedy route for UserRoute. Perhaps User/{username} might be a more workable route that doesn't tread on the toes of your second route?spender
I should have definitely mysite/abcde, not mysite/User/abcde. How to say ASP.NET MVC to generate a path "/Dashboard/Index" when I call return RedirectToAction("Index", "Dashboard"); ?Oleg Sh
@OlegSh Just think about what you are asking for. This means that anybody writing a new controller will need to know which users exist, and users will not be allowed to have usernames that match controller names. That sucks. Think really hard about this. It will be a pain. It will be pain.spender

1 Answers

0
votes

Your routing is correct (at least for the URLs you provided). But you are not calling the redirect correctly.

When you generate an outgoing route, it doesn't match a URL, it matches the route values that are passed. Your UserRoute contains 3 route values:

  • username
  • Controller
  • Action

So, in order to generate a URL (or redirect) based on this route, you need to pass all 3 parameters.

return RedirectToAction("Index", "Dashboard", new { username = "Fred" });

That said, MVC will automatically reuse route values if they are in the current request. So, if your route already has a username value (for example, you are already at the URL /Fred), it will be able to match the route without specifying the route value explicitly.

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