Компилируем razor файлы в одну dll (asp.net mvc)

  1. Указываем значение «Embedded Resource» в свойстве «Build Action» у всех файлов разметки.
  2. Создадим и зарегистрируем собственный VirtualPathProvider:
    public class EmbeddedVirtualPathProvider : VirtualPathProvider
    {
        public EmbeddedVirtualPathProvider()
        {
        }
        public override bool FileExists(string virtualPath)
        {
            return IsEmbeddedResourcePath(virtualPath) || base.FileExists(virtualPath);
        }
        public override VirtualFile GetFile(string virtualPath)
        {
        if (IsEmbeddedResourcePath(virtualPath))
            return new EmbeddedVirtualFile(virtualPath);
        else
            return base.GetFile(virtualPath);
        }
    
        public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart)
        {
        if (IsEmbeddedResourcePath(virtualPath))
            return null;
        else
            return base.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
        }
        public static void RegisterVirtualPathProvider()
        {
            HostingEnvironment.RegisterVirtualPathProvider(new EmbeddedVirtualPathProvider());
        }
    
        private bool IsEmbeddedResourcePath(string virtualPath)
        {
            string manifestResourceName = GetManifestResourceName(virtualPath);
            return GetType().Assembly.GetManifestResourceNames()
    .Where(o => o == manifestResourceName).FirstOrDefault() != null;
        }
        private static string GetManifestResourceName(string virtualPath)
        {
            if (virtualPath.StartsWith("~/"))
                virtualPath = virtualPath.Substring(1);
    
            string nameSpace = typeof(EmbeddedVirtualPathProvider).Assembly.GetName().Name;
    
            return nameSpace + virtualPath.Replace('/', '.');
        }
        private class EmbeddedVirtualFile : VirtualFile
        {
            private string _manifestResourceName;
    
            public EmbeddedVirtualFile(string virtualPath)
                : base(virtualPath)
            {
                _manifestResourceName = GetManifestResourceName(virtualPath);
            }
    
            public override System.IO.Stream Open()
            {
                return typeof(EmbeddedVirtualFile).Assembly.GetManifestResourceStream(_manifestResourceName);
            }
        }
    }
    
  3. В global.asax пропишем строчку:
    EmbeddedVirtualPathProvider.RegisterVirtualPathProvider();