1
votes

If I want to hit a url like http://localhost:8080/controllername I want the "Index" action to be the default action called. I assumed the default route mapping would be fine and the "Index" action would be called on whatever controller was specified - seems I need to specify http://localhost:8080/controllername/index

Is this correct?

Mapping:

 routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );
2
Can we see your other routes? Perhaps others are taking priority? - spender
I have this route set up and the url that's displayed to access this is localhost:4765 routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Customers", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); - hoakey
@hoakney I have normally used the same route too. I only have one route mapping as above and get a 404 I try to access localhost:8080/cms as apposed to localhost:8080/cms/index - Samuel Goldenbaum
@spender - no other routes mapped! - Samuel Goldenbaum
This looks like a problem with you server. You're running your own IIS as far as I can see. Try running the same app with VS's development server and report if you're still having the same problem. - gligoran

2 Answers

0
votes

What you're trying should definitely work. In fact, the code you posted is from the default templates, and I've just tested it by adding an "Index" action to the AccountController and visiting /Account in my browser.

I'd recommend creating a new project and testing this behaviour (first with the built-in web server, then with IIS, if you're not always using the built-in server). If it works, there's probably something different in your project that's causing the issue.

0
votes

I had a similar problem and it occured because of a collision with a directory in the project. I had a structure like this in my project:

Controllers \
             HomeController.cs
             CmsController.cs
Cms \
     WhateverFile.cs

The Cms subdirectory collided with the /Cms URL, while Cms/Index worked. I simply renamed my colliding folder name. If you have to keep it, there is a RouteCollection.RouteExistingFiles that can be used to prevent automatic lookup of files. If that is enabled I think that a lot exclusions have to be added for the Script etc, see this blog post for an example.