<%@ Page Language="C#" MasterPagePath="{CurrentTheme.SiteMaster}" Inherits="..." %>
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Site1.Master" Inherits="MvcHelperApplication.Inc.Types.ViewPage" %> <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"><h2>Index</h2></asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server"> </asp:Content>
<%@ Page Title="" Language="C#" MasterPagePath="{CurrentMasterPath}" Inherits="MvcHelperApplication.Inc.Types.ViewPage" %>
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace MvcHelperApplication.Inc { public class ControlHelper { private Types.ViewPage mPage = null; public ControlHelper(Types.ViewPage page) { mPage = page; } public void page_PreInit(object sender, EventArgs e) { try { if (mPage.MasterPagePath != null && mPage.MasterPagePath == "{CurrentMasterPath}") { mPage.MasterPageFile = "~/Views/Site1.Master"; } } catch (Exception ex) { } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Mvc.Html; using System.Web.UI; using System.Diagnostics.CodeAnalysis; namespace MvcHelperApplication.Inc.Types { public class ViewPage : System.Web.Mvc.ViewPage { private string _masterpagepath = ""; public string MasterPagePath { get { return _masterpagepath; } set { _masterpagepath = value; } } public ViewPage() { this.PreInit += new EventHandler(ControlHelper.page_PreInit); } private ControlHelper mControlHelper = null; public ControlHelper ControlHelper { get { if (mControlHelper == null) mControlHelper = new ControlHelper(this); return mControlHelper; } } } }
<%=ControlHelper.Config%> <%=ControlHelper.Themes.Current%>
: , . . : 'masterpagepath': 'System.Web.Mvc.ViewPage' 'masterpagepath'. : 1: <%@ Page Title="" Language="C#" MasterPagePath="{CurrentMasterPath}" Inherits="MvcHelperApplication.Inc.Types.ViewPage<dynamic>" %>
public class ViewPage<TModel> : System.Web.Mvc.ViewPage<TModel>
<pages validateRequest="false" pageParserFilterType="MvcHelperApplication.Inc.ViewTypeParserFilter" pageBaseType="MvcHelperApplication.Inc.Types.ViewPage">
using System; using System.Collections; using System.Web.UI; using System.Web.Mvc; using System.CodeDom; using System.Web.UI; namespace MvcHelperApplication.Inc { public class ViewTypeParserFilter : PageParserFilter { private string _viewBaseType; private DirectiveType _directiveType = DirectiveType.Unknown; private bool _viewTypeControlAdded; public override void PreprocessDirective(string directiveName, IDictionary attributes) { base.PreprocessDirective(directiveName, attributes); string defaultBaseType = null; switch (directiveName) { case "page": _directiveType = DirectiveType.Page; defaultBaseType = typeof(Types.ViewPage).FullName; break; case "control": _directiveType = DirectiveType.UserControl; defaultBaseType = typeof(System.Web.Mvc.ViewUserControl).FullName; break; case "master": _directiveType = DirectiveType.Master; defaultBaseType = typeof(System.Web.Mvc.ViewMasterPage).FullName; break; } if (_directiveType == DirectiveType.Unknown) return; string inherits = (string)attributes["inherits"]; if (!String.IsNullOrEmpty(inherits)) { if (IsGenericTypeString(inherits)) { attributes["inherits"] = defaultBaseType; _viewBaseType = inherits; } } } private static bool IsGenericTypeString(string typeName) { return typeName.IndexOfAny(new char[] { '<', '(' }) >= 0; } public override void ParseComplete(ControlBuilder rootBuilder) { base.ParseComplete(rootBuilder); ViewPageControlBuilder pageBuilder = rootBuilder as ViewPageControlBuilder; if (pageBuilder != null) { pageBuilder.PageBaseType = _viewBaseType; } } public override bool ProcessCodeConstruct(CodeConstructType codeType, string code) { if (codeType == CodeConstructType.ExpressionSnippet && !_viewTypeControlAdded && _viewBaseType != null && _directiveType == DirectiveType.Master) { Hashtable attribs = new Hashtable(); attribs["typename"] = _viewBaseType; AddControl(typeof(System.Web.Mvc.ViewType), attribs); _viewTypeControlAdded = true; } return base.ProcessCodeConstruct(codeType, code); } public override bool AllowCode { get {return true;} } public override bool AllowBaseType(Type baseType) { return true; } public override bool AllowControl(Type controlType, ControlBuilder builder) { return true; } public override bool AllowVirtualReference(string referenceVirtualPath, VirtualReferenceType referenceType) { return true; } public override bool AllowServerSideInclude(string includeVirtualPath) { return true; } public override int NumberOfControlsAllowed { get {return -1;} } public override int NumberOfDirectDependenciesAllowed { get {return -1;} } public override int TotalNumberOfDependenciesAllowed { get {return -1;} } private enum DirectiveType { Unknown, Page, UserControl, Master, } } public sealed class ViewPageControlBuilder : FileLevelPageControlBuilder { public string PageBaseType { get; set; } public override void ProcessGeneratedCode( CodeCompileUnit codeCompileUnit, CodeTypeDeclaration baseType, CodeTypeDeclaration derivedType, CodeMemberMethod buildMethod, CodeMemberMethod dataBindingMethod) { if (PageBaseType != null) { derivedType.BaseTypes[0] = new CodeTypeReference(PageBaseType); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Mvc.Html; using System.Web.UI; using System.Diagnostics.CodeAnalysis; namespace MvcHelperApplication.Inc.Types { [FileLevelControlBuilder(typeof(ViewPageControlBuilder))] public class ViewPage : System.Web.Mvc.ViewPage { private string _masterpagepath = ""; public string MasterPagePath { get { return _masterpagepath; } set { _masterpagepath = value; } } public ViewPage() { this.PreInit += new EventHandler(ControlHelper.page_PreInit); } private ControlHelper mControlHelper = null; public ControlHelper ControlHelper { get { if (mControlHelper == null) mControlHelper = new ControlHelper(this); return mControlHelper; } } } [FileLevelControlBuilder(typeof(ViewPageControlBuilder))] public class ViewPage<TModel> : ViewPage where TModel : class { // code copied from source of ViewPage<T> private ViewDataDictionary<TModel> _viewData; public new AjaxHelper<TModel> Ajax { get; set; } public new HtmlHelper<TModel> Html { get; set; } public new TModel Model { get { return ViewData.Model; } } [SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public new ViewDataDictionary<TModel> ViewData { get { if (_viewData == null) { SetViewData(new ViewDataDictionary<TModel>()); } return _viewData; } set { SetViewData(value); } } public override void InitHelpers() { base.InitHelpers(); Ajax = new AjaxHelper<TModel>(ViewContext, this); Html = new HtmlHelper<TModel>(ViewContext, this); } protected override void SetViewData(ViewDataDictionary viewData) { _viewData = new ViewDataDictionary<TModel>(viewData); base.SetViewData(_viewData); } } }
Source: https://habr.com/ru/post/134843/
All Articles