Alasdair Stark
Alasdair Stark

Reputation: 1281

Default routing to index not work for new controller

I have the following code for the navigation bar at the top of my application:

<li>@Html.ActionLink("Users", "Index", "Account")</li>
<li>@Html.ActionLink("Models", "Index", "Models")</li>

In AccountController I have the following Index function:

        [Authorize(Roles = "Admin, CanEditGroup, CanEditUser")]
        public ActionResult Index()
        {
            var users = _db.Users;
            var model = new List<EditUserViewModel>();
            foreach (var user in users)
            {
                var u = new EditUserViewModel(user);
                model.Add(u);
            }
            return View(model);
        }

In ModelsController I have the following Index function:

        public ActionResult Index()
        {
            return View();
        }

When I click on the 'Users' link I am taken to '/Account' and the Index is correctly shown. However, when I click the 'Models' link I am taken to '/Models' and receive a 'HTTP Error 403.14 - Forbidden' error. When I debug this I can see that the Index function in 'ModelsController' isn't being hit. '/Models/Index' works fine though.

It should be noted that I am using a template where AccountController already existed. I have since added ModelsController.

Finally, here is my simple routing file:

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

Please help me understand the difference between AccountController and ModelsController which is causing a difference in routing behaviour.

Upvotes: 1

Views: 908

Answers (1)

marathonman
marathonman

Reputation: 452

i think you already have a models folder or some other resource with named models in your project. you can change your controller name.

or set RouteExistingFiles for your route collections

routes.RouteExistingFiles = true;

will help

Upvotes: 5

Related Questions