Developer forum

Forum » Development » MVC : duplicate routes

MVC : duplicate routes

Gaëtan Di Caro
Reply

Hello,

I'm trying to add an MVC controller to my solution. I've created a folder called a folder called "controller" where I've put my controller, the default class "RouteConfig" which is there when you create a default mvc application, and edited my global.asax to register the areas and the routes.

However I get this exception in Application_BeginRequest when I try to run my website :

System.ArgumentException: 'A route named 'AuthLogin' is already in the route collection. Route names must be unique.'

I've tried some links :

http://doc.dynamicweb.com/documentation-8/how-tos/install-setup/setting-up

http://doc.dynamicweb.com/forum/dynamicweb-9-0-upgrade-issues/using-webapi-with-attribute-routing-in-dw-9-dynamicweb-with-asp-net-mvc

And of course i googled the error extensively, but I can't seem to solve it.

Here's my Application_Start :

public void Application_Start(object sender, EventArgs e)
        {
            // Fires when the application is started
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);

            GlobalAsaxHandler.Application_Start(sender, e);
        }

RouteCOnfig.cs :

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

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

My controller :

 public class ConfiguratorController : Controller
    {
        public ActionResult GetProduct()
        {
            return Json("hello");
        }
    }

 

If I remove these two rows from my global.asax, the website runs, but I can't access my controller :

AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);

 

I run 9.2.17.

Any idea ?

 

Thanks


Replies

 
Nicolai Pedersen
Reply

Hi Gaetan

Attached find my local setup of this - with MVC and WEB api in one solution. The solution is covered in this video as well: https://vimeo.com/217447832

See dumps where you can see I am accessing /test for an MVC controller and /api/testing for a WEB Api controller.

Capture.PNG Capture1.PNG
 
Gaëtan Di Caro
Reply

Thanks Nicolai. I understand now that I don't need all the global asax code nor the RouteConfig so i removed that.

I followed the video step by step but I can't seem to access my controller. If I go to http://local.mysite.dk/Configurator/GetProduct, I'm simply shown the front page of the website. I don't understand why, it should work out of the box. I tried mvc controller and webapi controller, same result.

I could not get to try your solution as I get an error with Nuget that I can't seem to make go away.

 

 
Nicolai Pedersen
Reply

You should be able to get the info out the project anyways. And get it to work!

I'll be nice and try to help you out - zip your project files (Not bin and all that data) so I can take a look at your code.

BR Nicolai

 
Gaëtan Di Caro
Reply

Yes I opened the project and inspected the files still.

That said, since I couldn't understand why my setup wasn't working I just copied the test controller and view from your project into my solution to find that they actually work there... Turns out that the controller needs a default action. If I simply add "[Route("{action=GetProduct}")]" to my controller, it works.

 
Nicolai Pedersen
Reply

Great - glad you got it working!

 

You must be logged in to post in the forum