I try to write my own CMS using ASP.NET MVC and I store html views in DB. I created custom virtual path provider by tutorial in the internet.
MyVirtualPathProvider Code:
using System.Linq;
using System.Text;
using System.Web.Hosting;
using SgCms.Dal.Concrete;
using SgCms.DomainModels;
namespace SgCms.ViewFactory
{
public class SgCmsVirtualPathProvider : VirtualPathProvider
{
public override bool FileExists(string virtualPath)
{
var view = GetViewFromDatabase(virtualPath);
if (view == null)
{
return base.FileExists(virtualPath);
}
else
{
return true;
}
}
public override VirtualFile GetFile(string virtualPath)
{
var view = GetViewFromDatabase(virtualPath);
if (view == null)
{
return base.GetFile(virtualPath);
}
else
{
byte[] content = ASCIIEncoding.ASCII.
GetBytes(view.InnerHtml);
return new SgCmsVirtualFile
(virtualPath, content);
}
}
private View GetViewFromDatabase(string virtualPath)
{
return PageBuilder(virtualPath);
}
private View PageBuilder(string path)
{
var virtualPath = path.Replace("~", "");
DataContext db = new DataContext();
var view = from v in db.Views
where v.Path == virtualPath
select v;
var res = view.SingleOrDefault();
return res;
}
}
}
MyVirtualFile Code:
using System.IO;
using System.Web.Hosting;
namespace SgCms.ViewFactory
{
public class SgCmsVirtualFile : VirtualFile
{
private byte[] viewContent;
public SgCmsVirtualFile(string virtualPath,
byte[] viewContent) : base(virtualPath)
{
this.viewContent = viewContent;
}
public override Stream Open()
{
return new MemoryStream(viewContent);
}
}
}
I want to get views from DB by path like that: http://localhost/Module_Name/View_Name
I wrote my custom route and now all requests handle by one action. Even, when GetViewFromDatabase is called it get view which I want. But, unfortunately I have next exception:
Directory 'c:\users\nikita litvinenko\documents\visual studio 2017\Projects\SgCms\SgCms.Web\Views\Main' does not exist. Failed to start monitoring file changes. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: Directory 'c:\users\nikita litvinenko\documents\visual studio 2017\Projects\SgCms\SgCms.Web\Views\Main' does not exist. Failed to start monitoring file changes.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below Stack Trace: [HttpException (0x80070002): Directory 'c:\users\nikita litvinenko\documents\visual studio 2017\Projects\SgCms\SgCms.Web\Views\Main' does not exist. Failed to start monitoring file changes.] System.Web.FileChangesMonitor.FindDirectoryMonitor(String dir, Boolean addIfNotFound, Boolean throwOnError) +589 System.Web.FileChangesMonitor.StartMonitoringPath(String alias, FileChangeEventHandler callback, FileAttributesData& fad) +655 System.Web.Caching.CacheDependency.Init(Boolean isPublic, String[] filenamesArg, String[] cachekeysArg, CacheDependency dependency, DateTime utcStart) +2582 System.Web.Hosting.MapPathBasedVirtualPathProvider.GetCacheDependency(String virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart) +400 System.Web.Hosting.VirtualPathProvider.GetCacheDependency(String virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart) +36 System.Web.Hosting.VirtualPathProvider.GetCacheDependency(VirtualPath virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart) +38 System.Web.Compilation.MemoryBuildResultCache.CacheBuildResult(String cacheKey, BuildResult result, Int64 hashCode, DateTime utcStart) +68 System.Web.Compilation.BuildManager.CacheBuildResultInternal(String cacheKey, BuildResult result, Int64 hashCode, DateTime utcStart) +70 System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath) +10180802 System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate) +294 System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate) +103 System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean throwIfNotFound) +165 System.Web.Compilation.BuildManager.GetCompiledType(VirtualPath virtualPath) +15 System.Web.Compilation.BuildManager.GetCompiledType(String virtualPath) +29 System.Web.Mvc.BuildManagerWrapper.System.Web.Mvc.IBuildManager.GetCompiledType(String virtualPath) +6 System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +54 System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +291 System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList
1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +56 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList1 filters, ActionResult actionResult) +52 System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +173 System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100 System.Web.Mvc.Async.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +49 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27 System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +29 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +49 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +36 System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +22 System.Web.Mvc.Async.WrappedAsyncResultBase1.End() +49 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10 System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21 System.Web.Mvc.Async.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +29 System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9765901 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155