PartialViewResult not bird _ ViewStart. cshtml

Source: Internet
Author: User

_ 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 );}}

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.