开发者

MVC Controller named Controllers throws "Server Error in '/' Application."

I'm using ASP.NET MVC 3 (Razor) and have created a Controller named "Controllers" - specifically, the Controller class name is "ControllersController".

Here's a snippet of my "Controllers" Controller:

public class ControllersController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

In addition to creating the ControllersController class, I created a Razor View (Index.cshtml) that correlates with the ControllersController Index() action. It may be worth noting that I created the Index.cshtml View by right-clicking the Index() method within the Controller and choosing "Add View".

The problem that I am experiencing is, when a browser tries to go to http://localhost/controllers, the following error is thrown:

Server Error in '/' Application.

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 c开发者_Go百科hanged, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /controllers/

There are two points that I'd like to point out:

  1. The error can be avoided if the browser explicitly goes to http://localhost/controllers/index
  2. Controllers that are not named ControllersController don't force the browser to specify the /index action in its URL.

With all the naming conventions in MVC, I'm not surprised that a Controller named Controllers causes some strange behavior. My question is; what do I need to do so that I don't have to specify /index in the browser's URL?


This is because you have a folder called "Controllers" on disk in your project. One option would be to rename that folder to something else. This problem won't exist when you publish your site using the Visual Studio publish option because the Controllers folder won't get pushed. A second more cumbersome solution would be to set routes.RouteExistingFiles = true; in the RegisterRoutes function in your global.asax.cs. You then may want to call route.IgnoreRoutes for items in your content folder, etc.


Stay away from using any current names in your code - the world will be a better place, children wont cry, etc.

"With all the naming conventions in MVC, I'm not surprised that a Controller named Controllers causes some strange behavior. "

If it feels weird - don't do it : )


Apparently, the problem has nothing to do with the name of the Controller (i.e. ControllersController), but rather with the name of the URL route.

In my Global.asax.cs, I added a custom route as follows:

public static void RegisterRoutes(RouteCollection routes)
{
    //Route /controllers/ to /Home/Index
    routes.MapRoute(
        "Controllers",
        "controllers/{action}/{id}",
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

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

Notice the the custom route (named "Controllers") routes to the HomeController (not ControllersController).

I would expect that when the browser points to http://localhost/controllers, MVC would return the /Home/Index page. Instead, a 404 error is thrown. However, the route succeeds if the browser points to http://localhost/controllers/index. - This is the same symptoms as I mentioned at the beginning of this post.

Based on this test, I think it's safe to assume that the problem is not with the naming convention of the Controller, but rather with the naming convention of the MVC routing.

To move on with building my application, I'm going to change the name of my ControllersController to ControllerSystemsController. In the meantime, I'm interested to know specifically why the MVC router doesn't work as expected for routes that use "controllers" as the name of the controller. Can anyone shed light on this?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜