Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Controller Index sometimes 404?

I've added 3 controllers, and for one of them the Index view doesn't work by default.

Works(shows index):
http://localhost:1767/Employees/  
http://localhost:1767/Employees/Index
http://localhost:1767/Home/       
http://localhost:1767/Home/Index
http://localhost:1767/
http://localhost:1767/Companies/Index

Doesn't work(gives 404 error):
http://localhost:1767/Companies/  

1) I created an entity framework class library, generated from a database containing a Employees and Companies table.

2) Created a MVC 3 empty project.

3)Added a project reference from the MVC project to the EF class library project.

4)Added a controller, and in the add controller dialog I chose the EF model and one of the tables and named the controller CompaniesController

5)Then I remembered I needed a HomeController, so I added another controller, naming it HomeController, and again chose the Companies entity.

6)Finally I added a EmployeesController choosing the Employees entity.

This is the only route in my Global.asax.cs:

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

Why doesn't the http://localhost:1767/Companies/ URL default to the index action like all of the other controllers? I have done a few practice MVC projects and seem to always have this problem. The Employees controller defaulting to Index works fine, even though there is no route that tells it to do this?

Edit: Here is the error I receive:

The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly.

Requested URL: /Companies/

Edit: So I usually don't reveal the names of my entities or domain details when posting examples online, but in this case Companies was really called Properties, and changing the name to Properties2 for the view folder and controller fixed the issue. So it seems Properties is a special keyword that breaks the default to Index if it appears in a URL

like image 387
AaronLS Avatar asked Jul 12 '11 22:07

AaronLS


3 Answers

Changed the view folder and controller from Properties(Controller) to Properties2(Controller) and it works fine now. Apperently "Properties" has some special handling in a URL and breaks the defaulting to index

like image 50
AaronLS Avatar answered Nov 20 '22 12:11

AaronLS


Did you add an Index action to your CompaniesController?

[HttpGet]
public ActionResult Index()
{
    return View();
}
like image 38
Rodrigo Caballero Avatar answered Nov 20 '22 14:11

Rodrigo Caballero


I received 403.1 because I had folder name same as controller name under web project root directory. So even before it reaches MVC route resolution, it fails trying to list folder content. May be similar issue with properties being standard folder in any project

like image 3
Lokeshwer Avatar answered Nov 20 '22 14:11

Lokeshwer