views:

145

answers:

3

If I'm not mistaken - the conventions of ASP.NET MVC seem to want me to do the following for a controller view.

Thats to day I create 'Products' directory into which I place my 'Index' view. I then create a 'ProductsController' and create an 'Index' method on it which returns a View. Returning just View() with no arguments will go and fetch the 'Index.aspx' page because its the same name as the method.

 public class ProductsController : Controller
    {
        public ActionResult Index()
        {
            return View();   // looks for Index.aspx in Products directory
        }
    }

Now thats just fine. BUT I'll end up with a billion Index.aspx pages, and I'm one of these people that never closes any files so I'll end up going crazy.

Alternatively I can create Products/Products.aspx and change my controller to the following :

 public class ProductsController : Controller
    {
        public ActionResult Index()  // my default routing goes to Index (from sample project)
        {
            return View("Products");
        }
    }

I understand how that works, and that its completely fine within the MVC design pattern to do this. Its not a hack or anything like that.

My problem (after listening to this PDC video) is that convention over customizabiltity is favored in MVC (or whatever the correct phrase is).

So I'm wondering if I'm missing a third way, or if people are just fine with 50 Index tabs in Visual Studio?

A: 

It's just a pattern. Use the pattern, but make it work for you. Personally, I like to have my Views named by their function and I don't have many Index.aspx pages, because I don't have many indices.

tvanfosson
but surely the idea of 'index.aspx' is just to be the same as the 'standard' index.htm or index.html convention. its not about any kind of index. they could have called it Default which i actually think is a better name for the method, but MS probably wanted to get away from default.aspx
Simon_Weaver
+2  A: 

I think you should name the method after the action and name the view (if it makes sense and it's not shared between actions, the same as the action). You should probably change your routing mechanism as Index isn't really a descriptive name. The action name should represent what it does (just like any method) and shouldn't be hardcoded to Index or something like that. Routing should be edited instead.

Mehrdad Afshari
i havent got into routing yet! i'll look at in more depth, but i think understanding it is definitely going to teach me a lot. just trying to learn MVC today and reformat a coworkers laptop for him at the same time
Simon_Weaver
+2  A: 

Having the default action for each controller have the same name just simplifies the routing (check global.asax). Also, it all things (Products, Books, Contacts, ...) use actions/views with the same name for the same function, then the code becomes much easier to navigate and understand. This use of convention is especially important when working as part of a team as it will encourage consistent code across developers.

While looking at another question, I ran across SimplyRestfulRouting in the MVCContrib project on codeplex. This might give you some ideas.

Matthew
so you dont mind tonnes of Index.aspx pages?
Simon_Weaver
Why not? they are in different directories and are performing an Index ( or List) function. Why not call all the views that display a list of thing Index (or List).
Matthew
+1 Matthew, being consistent in your routes is the best thing to do and taking advantage of the functionality the SimplyRestfulRouting exposes helps you do that. I already have the mind set, that when I see an Index that it is the default route and view.
Dale Ragan
i've gotten used to lots of 'index' files. definitely simplifies routing, but you can of course use [ActionName("Index")] and call your method something else, and then return a view of a differnet name. i've settled on the parretn though
Simon_Weaver