views:

750

answers:

2

I'm confused with how views are organized.

Under the views directory, there are subdirectories. Inside these subdirectories are views. I'm assuming that the subdirectories map to controllers, and the controllers act on the views contained within their subdirectories.

Is there an emerging expectation of what types of views are contained within these directories? For instance, should the default page for each directory be index.aspx? Should the pages follow a naming convention such as Create[controller].aspx, List[controller].aspx, etc?

+5  A: 

View directory naming and file naming are important, because the ASP.NET MVC framework makes certain assumptions about them. If you do not conform to these assumptions, then you must write code to let the framework know what you are doing. Generally speaking, you should conform to these assumptions unless you have a good reason not to.

Let's look at the simplest possible controller action:

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

Because no view name has been specified in the call to View(), the framework will presume that the view filename will be the same as the Action name. The framework has a type called ViewEngine which will supply the extension. The default ViewEngine is WebFormViewEngine, which will take that name and append an .aspx to it. So the full filename in this case would be NotAuthorized.aspx.

But in which folder will the file be found? Again, the ViewEngine supplies that information. With WebFormViewEngine, it will look in two folders: ~/Views/Shared and ~/Views/{controller}

So if your controller was called AccountController, it would look in ~/Views/Account

But there might be times when you don't want to follow these rules. For instance, two different actions might return the same view (with a different model, or something). In this case, if you specify the view name explicitly in your action:

    public ActionResult NotAuthorized()
    {
        return View("Foo");
    }

Note that with WebFormViewEngine, the "view name" is generally the same as the filename, less the extension, but the framework does not require that of other view engines.

Similarly, you might also have a reason to want your application to look for views and non-default folders. You can do that by creating your own ViewEngine. I show the technique in this blog post, but the type names are different, since it was written for an earlier version of the framework. The basic idea is still the same, however.

Craig Stuntz
Can the folders within the View have subfolders? if so, how does the Controller reach them? For example... Admin/Profile/Edit/1
Eric Brown
You would have to write your own ViewEngine to do this. WebFormViewEngine won't find them.
Craig Stuntz
@Eric Setting `ViewLocationFormats` on the Web Form view engine instance is all that's needed to enable putting views in subfolders.
bzlm
+1  A: 

In regard to expected names for the views, I think that it's one of those things that each project or organization will try to standardize.

As you hinted to in your question, it's possible that some of these Views (or more precisely, the Actions that render them) become popular across the board, like for example the ones below that are common in RoR applications that adopt the REST paradigm:

  • /orders/ (i.e. index)
  • /orders/show/123
  • /orders/edit/123
  • /orders/update/123
  • /orders/new
  • /orders/create
  • /orders/destroy/123

The choice/standardization of the Views is largely dependent on how you model your application (to say the obvious) and how fine-grained you want to go. The closer you map your controllers to individual model classes (cough...resources...cough), the shorter your actions will tend to be and more easily you will be able to follow a standard set of actions (as in the above example).

I also believe that shorter actions help pushing more and more of the model business logic into the models themselves, where it belongs.

sergiopereira