_ ViewStart. cshtml.
public class HomeController : Controller { public ActionResult Index() { return View("Index"); } public ActionResult PartView() { return PartialView("Index"); } }
(1)If the view is set as follows "~ /Views/Shared/_ Layout. cshtml "; defined in Index. cshtml.
(2)If the view is set as follows "~ /Views/Shared/_ Layout. cshtml "; defined in _ ViewStart. cshtml.
(Because the code on the motherboard page is not executed in the second case)To be more convincing, let's take a look at the source code of ASP. net mvc and compare ViewResult and PartialViewResult to check whether our conjecture is correct !!
Public abstract class ViewResultBase: ActionResult {// omit other code... public override void ExecuteResult (ControllerContext context) {if (context = null) {throw new ArgumentNullException ("context");} if (string. isNullOrEmpty (this. viewName) {this. viewName = context. routeData. getRequiredString ("action");} ViewEngineResult viewEngineResult = null; if (this. view = null) {// execute the FindView method (implemented in a derived class) and create a View object through the View engine! ViewEngineResult = this. findView (context); this. view = viewEngineResult. view;} TextWriter output = context. httpContext. response. output; ViewContext viewContext = new ViewContext (context, this. view, this. viewData, this. tempData, output); this. view. render (viewContext, output); if (viewEngineResult! = Null) {viewEngineResult. ViewEngine. ReleaseView (context, this. View) ;}// this method implements protected abstract ViewEngineResult FindView (ControllerContext context) in ViewResult and PartialViewResult of the derived classes );}
When using the Render (viewContext, output) method,
Public abstract class BuildManagerCompiledView: IView {// omit other code... public void Render (ViewContext viewContext, TextWriter writer) {if (viewContext = null) {throw new ArgumentNullException ("viewContext");} object obj = null; Type compiledType = this. buildManager. getCompiledType (this. viewPath); if (compiledType! = Null) {obj = this. _ viewPageActivator. create (this. _ controllerContext, compiledType);} if (obj = null) {throw new InvalidOperationException (string. format (CultureInfo. currentCulture, MvcResources. cshtmlView_ViewCouldNotBeCreated, new object [] {this. viewPath});} this. renderView (viewContext, writer, obj);} // implement protected abstract void RenderView (ViewContext viewContext, TextWriter writer, object insta) in the derived class Nce);} public class RazorView: BuildManagerCompiledView {// omit other code... protected override void RenderView (ViewContext viewContext, TextWriter writer, object instance) {if (writer = null) {throw new ArgumentNullException ("writer");} WebViewPage webViewPage = instance as WebViewPage; if (webViewPage = null) {throw new InvalidOperationException (string. format (CultureInfo. currentCulture, MvcResources. cshtmlVi Ew_WrongViewBase, new object [] {base. viewPath});} webViewPage. overridenLayoutPath = this. layoutPath; webViewPage. virtualPath = base. viewPath; webViewPage. viewContext = viewContext; webViewPage. viewData = viewContext. viewData; webViewPage. initHelpers (); if (this. virtualPathFactory! = Null) {webViewPage. VirtualPathFactory = this. VirtualPathFactory;} if (this. DisplayModeProvider! = Null) {webViewPage. displayModeProvider = this. displayModeProvider;} WebPageRenderingBase startPage = null; // here we are here .... if (this. runViewStartPages) {// execute "_ ViewStart. content in cshtml! StartPage = this. startPageLookup (webViewPage, RazorViewEngine. viewStartFileName, this. viewStartFileExtensions);} WebPageBase arg_D3_0 = webViewPage; HttpContextBase httpContext = viewContext. httpContext; WebPageRenderingBase page = null; object model = null; arg_D3_0.ExecutePageHierarchy (new WebPageContext (httpContext, page, model), writer, startPage );}}