/// <summary> /// Registers a View Model and associated View. /// </summary> /// <param name="viewName">The name of the View to register.</param> /// <param name="modelType">The View Model Type to associate with the View. Must be a subclass of Type <see cref="ViewModel"/>.</param> /// <param name="controllerName">The Controller name. If not specified (or <c>null</c>), the Controller name is inferred from the <see cref="modelType"/>: either "Entity", "Region" or "Page".</param> private static void RegisterViewModel(string viewName, Type modelType, string controllerName = null) { if (String.IsNullOrEmpty(controllerName)) { if (typeof(EntityModel).IsAssignableFrom(modelType)) { controllerName = "Entity"; } else if (typeof(RegionModel).IsAssignableFrom(modelType)) { controllerName = "Region"; } else { controllerName = "Page"; } } MvcData mvcData = new MvcData(viewName) { ControllerName = controllerName }; ModelTypeRegistry.RegisterViewModel(mvcData, modelType); }
/// <summary> /// Registers this View Model Type. /// </summary> /// <remarks> /// Although this View Model Type is part of the DXA Framework, it has to be registered like any other View Model Type. /// In order to work with Tridion Docs content, it will be associated with specific MVC data. /// A DXA Web Application/Module that wants to work with Tridion Docs content should call this method /// unless it defines its own View Model Type for generic Topics. /// </remarks> public static void Register() { using (new Tracer()) { ModelTypeRegistry.RegisterViewModel(new MvcData("Ish:Entity:Topic"), typeof(GenericTopic)); } }
/// <summary> /// Automatically register all view models for an area. This is done by searching the file system /// for all .cshtml files, determining the controller and view names from the path, and using the /// BuildManager to determine the model type by compiling the view. Note that if your area contains /// a lot of views, this can be a lengthy process and you might be better off explicitly registering /// your views with the RegisterViewModel method /// </summary> protected virtual void RegisterAllViewModels() { DateTime timer = DateTime.Now; int viewCount = 0; string baseDir = AppDomain.CurrentDomain.BaseDirectory; string path = Path.Combine(baseDir, "Areas", this.AreaName, "Views"); Log.Debug("Staring to register views for area: {0}", this.AreaName); foreach (string file in Directory.GetFiles(path, "*.cshtml", SearchOption.AllDirectories)) { string relativePath = file.Substring(path.Length + 1); string virtualPath = @"~/" + file.Replace(baseDir, string.Empty).Replace("\\", "/"); int pos = relativePath.IndexOf("\\"); if (pos > 0) { string controller = relativePath.Substring(0, pos); string view = relativePath.Substring(pos + 1); int extnPos = view.LastIndexOf(".cshtml"); view = view.Substring(0, extnPos); MvcData mvcData = new MvcData { AreaName = this.AreaName, ControllerName = controller, ViewName = view }; try { ModelTypeRegistry.RegisterViewModel(mvcData, virtualPath); viewCount++; } catch { //Do nothing - we ignore views that are not strongly typed } } else { Log.Warn("Cannot add view {0} to view model registry as it is not in a {ControllerName} subfolder", file); } } Log.Info("Registered {0} views for area {1} in {2} milliseconds. This startup overhead can be reduced by explicitly registering view using the Sdl.Web.Mvc.Configuration.BaseAreaRegistration.RegisterView() method.", viewCount, this.AreaName, (DateTime.Now - timer).TotalMilliseconds); }
public static void AddViewModelToRegistry(MvcData mvcData, Type modelType) { ModelTypeRegistry.RegisterViewModel(mvcData, modelType); }
public static void AddViewModelToRegistry(MvcData viewData, string viewPath) { ModelTypeRegistry.RegisterViewModel(viewData, viewPath); }
/// <summary> /// Registers a View Model without associated View. /// </summary> /// <param name="modelType">The View Model type.</param> /// <remarks> /// </remarks> protected void RegisterViewModel(Type modelType) { ModelTypeRegistry.RegisterViewModel(null, modelType); }
/// <summary> /// Registers a View Model Type without associated View. /// </summary> private static void RegisterViewModel(Type modelType) { ModelTypeRegistry.RegisterViewModel(null, modelType); }