R – Simple MVC routing problem

asp.net-mvcmodel-view-controllerroutingurl-routing

I have few pages that quite similar to the others but one of them doesn't work.

When I write 'http://localhost:2265/Segment/' I get annoying error message "Server Error in '/' Application.
The resource cannot be found."

Other pages like 'http://localhost:2265/User/' works very well AND also 'http://localhost:2265/Segment/Create'. So Index of the Segment is the problem. I have used ASP.NET Routing Debugger and on other pages I get correct mappings, but I get this same error message "resource cannot be found" also when using debugger.. I think this indicates that Default route doesn't catch it either..

Any ideas?

Here is my MapRoute commands.

            routes.MapRoute(
            "Maintenance",
            "Maintenance/{action}",
            new { controller = "Maintenance", action = "Index", id = "" }
        );

        routes.MapRoute(
            "User",
            "User/{action}",
            new { controller = "User", action = "Index", id = "" }
        );

        routes.MapRoute(
            "Segment",
            "Segment/{action}",
            new { controller = "Segment", action = "Index", id = "" }
        );

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

Update:

Thank you for the quick reply!

I removed all routes except the default. It didn't solve the problem, but now the route list is shorter.

I have other classes inside my controller file like this:

public class SegmentFormViewModel
{
}

public class SegmentController : Controller
{
}

public class SegmentFormCreateModel : Segment
{
}

I tried to move it inside controller, but that didn't help either.

Is there any way to debug this problem?

Update:

Here is my controller (without contents of the methods)

public class SegmentController : Controller
{
    //
    // GET: /Segment/

    public ActionResult Index()
    {
    }

    //
    // GET: /Segment/Details/5

    public ActionResult Details(Guid id)
    {
    }

    //
    // GET: /Segment/Create

    public ActionResult Create()
    {
    }

    //
    // POST: /Segment/Create

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(FormCollection collection)
    {
    }

    //
    // GET: /Segment/Edit/5

    public ActionResult Edit(int id)
    {
    }

    //
    // POST: /Segment/Edit/5

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(int id, FormCollection collection)
    {
    }
}

Best Answer

To match your /Segment/ route you will need a "SegmentController" controller class to have an "Index" action that accepts GET requests, ie not restricted with [AcceptVerbs(HttpVerbs.Post)].

As Darin has already commented, the default route would handle all the other routes you have added so you don't need the extra routes.

After update:
Your problem is probably more to do with the actions in the Segment controller. It doesn't matter what classes you have in which file. What actions do you have in the Segment controller?

After 2nd update:
Your actions look ok so I suspect the problem is in code you have not listed.
Next steps:
1. Use the router debugger already mentioned.
2. Download the MVC source so you can step through it.
Last resort: Start a brand new project and only add the Segment controller. Then keep adding related code until you find the problem.