Selecting a view to render

By Default:

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

Calling the View method returns a ViewResult object that knows how to render a particular view. When this method is called with no arguments, the framework infers that the name of the view to render should be the same as the name of the action (Create). Later in the MVC pipeline, the framework’s ControllerActionInvoker class executes the ViewResult and tells it to render the view. At this point, the framework asks the ViewEngineCollection to locate the appropriate view for rendering. (By default the view engine will look for views within the Views/<Controller Name> directory and the Views/Shared directory)

Overriding the view name:

You can override the convention for using the action name as the view name if you want to. For example, if your view was called New.cshtml rather than Create.cshtml, you could call a second overload of the View method that accepts an explicit view name:

return View("New");

Alternatively, you can specify an application-relative path to the view if it doesn’t reside within the subdirectory with the same name as the controller:

return View("~/Views/SomeOtherDirectory/New.cshtml");

 

View engines

By default, ASP.NET MVC ships with two view engines—the RazorViewEngine and the WebFormViewEngine. The Razor view engine is responsible for rendering views in the Razor format (either .cshtml files or .vbhtml files), while the Web Form view engine is used to support the older-format Web Form views (.aspx and .ascx files). Previous versions of ASP.NET MVC only included the Web Form view engine by default.

posted @ 2012-08-15 11:03  HelloWorld.Michael  阅读(149)  评论(0编辑  收藏  举报