Sdl.Web.Common/Models/ModelTypeRegistry.cs
/// <summary>
/// Registers a View Model mapping by compiling a given view file and obtaining its model type.
/// </summary>
/// <param name="viewData">The data for the View to register.</param>
/// <param name="viewVirtualPath">The (virtual) path to the View file.</param>
Registers a View Model mapping by compiling a given view file and obtaining its model type.
Registers a View Model mapping by compiling a given view file and obtaining its model type .
public static void RegisterViewModel(MvcData viewData, string viewVirtualPath)
        {
            using (new Tracer(viewData, viewVirtualPath))
            {
                lock (_viewToModelTypeMapping)
                {
                    if (_viewToModelTypeMapping.ContainsKey(viewData))
                    {
                        Log.Warn("View '{0}' registered multiple times. Virtual Path: '{1}'", viewData, viewVirtualPath);
                        return;
                    }
                    try
                    {
                        Type compiledViewType = BuildManager.GetCompiledType(viewVirtualPath);
                        if (!compiledViewType.BaseType.IsGenericType)
                        {
                            throw new DxaException("View is not strongly typed. Please ensure you use the @model directive.");
                        }
                        RegisterViewModel(viewData, compiledViewType.BaseType.GetGenericArguments()[0]);
                    }
                    catch (Exception ex)
                    {
                        throw new DxaException(String.Format("Error occurred while compiling View '{0}'", viewVirtualPath), ex);
                    }
                }
            }
        }
public static void RegisterViewModel ( MvcData viewData , string viewVirtualPath ) { using ( new Tracer ( viewData , viewVirtualPath ) ) { lock ( _viewToModelTypeMapping ) { if ( _viewToModelTypeMapping . ContainsKey ( viewData ) ) { Log . Warn ( " " , viewData , viewVirtualPath ) ; return ; } try { Type compiledViewType = BuildManager . GetCompiledType ( viewVirtualPath ) ; if ( ! compiledViewType . BaseType . IsGenericType ) { throw new DxaException ( " " ) ; } RegisterViewModel ( viewData , compiledViewType . BaseType . GetGenericArguments ( ) [ 0 ] ) ; } catch ( Exception ex ) { throw new DxaException ( String . Format ( " " , viewVirtualPath ) , ex ) ; } } } }
Registers a View Model mapping by compiling a given view file and obtaining its model type.
Registers a View Model mapping by compiling a given view file and obtaining its model type .
viewData
MvcData
The data for the View to register.
The data for the View to register .
viewVirtualPath
string
The (virtual) path to the View file.
The ( virtual ) path to the View file .