Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parameter only route always being triggered MVC3

I have two very simple routes

       routes.MapRoute(
            "post", // Route name
            postPage + "/{slug}", // URL with parameters
            new { controller = "Home", action = "Article" } // Parameter defaults
        );

        routes.MapRoute(
            "page", // Route name
            "{slug}", // URL with parameters
            new { controller = "Home", action = "Page", slug = homePage} // Parameter defaults
        );

And here is my controller logic

    public ActionResult Article(string slug)
    {
        return View(repo.GetPost(slug));
    }

    public ActionResult Page(string slug)
    {
        if (slug.ToLower() == MetaData.PostsPage.ToLower())
            return View("listPosts", repo.GetAllPosts());
        else
            return View("page", repo.GetPage(slug));
    }

homePage and postPage are set from value's in the database. Allowing the user to define the default page as well as the page to show posts.

My issue occurs when adding an area named "Admin". I get a controller added to my RouteTable

    context.MapRoute(
            "Admin_default",
            "Admin/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );

Now when a user Access Admin/Account/Logon the page loads fine, but my debugger still tries to go into the Home controller and the Page action. But the RouteDebugger says it doesn't match the current request. I'm puzzled on how to fix this.

RouteDebugger screenshot: https://i.sstatic.net/7cpHm.png Debugger going into my HomeControler Page AtionResult: https://i.sstatic.net/uSJBK.png

like image 834
Boek Avatar asked Dec 12 '25 20:12

Boek


2 Answers

Actually the problem is, Area routes are overriding the global routes, to distinguish both the routes set the relevant namespace of area's controller in the context.MapRoute method in adminAreaRegistraton.cs file. i.e.

context.MapRoute(
 "admin_default",
 "admin/{controller}/{action}/{id}",
 new { controller = "Home", action = "Index", id = UrlParameter.Optional },
 null,
 new string[] { "MVCApplication1.Areas.admin.Controllers" }
);
like image 149
Kundan Singh Chouhan Avatar answered Dec 14 '25 09:12

Kundan Singh Chouhan


I found out the issue.

I had a favicon.ico set in the main area of my site, but not the Admin area. So when I went to the Admin area the browser made a request for favicon.ico that got picked up by that route. Thats why my routes looked fine in the RouteDebugger, because they were.

Thanks for the help Kundan!

like image 38
Boek Avatar answered Dec 14 '25 10:12

Boek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!