Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 在VirtualPathProvider中未调用MVC GetFile方法_Asp.net Mvc_Virtualpathprovider_Razorengine - Fatal编程技术网

Asp.net mvc 在VirtualPathProvider中未调用MVC GetFile方法

Asp.net mvc 在VirtualPathProvider中未调用MVC GetFile方法,asp.net-mvc,virtualpathprovider,razorengine,Asp.net Mvc,Virtualpathprovider,Razorengine,我已经创建了一个VirtualPathProvider来读取部分视图(来自Azure存储),但是当我到达希望VirtualPathProvider查找视图的页面时(在Azure存储中),它不会调用GetFile方法。因此它找不到该文件。FileExists方法不会被调用并返回true 以下是包含我要加载的视图的页面: @model VTSMVC.Models.Controls.ControlData @{ Layout = "~/Views/Shared/_Layout.cshtml"

我已经创建了一个VirtualPathProvider来读取部分视图(来自Azure存储),但是当我到达希望VirtualPathProvider查找视图的页面时(在Azure存储中),它不会调用GetFile方法。因此它找不到该文件。FileExists方法不会被调用并返回true

以下是包含我要加载的视图的页面:

@model VTSMVC.Models.Controls.ControlData

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@{
    ViewBag.Title = @Model.PageTitle;
}

<h1>@ViewBag.Title</h1>

<div class="stockReportContainer">

    @Html.Partial(@Model.ViewCompName, @Model)

</div>
最后是VirtualPathProvider:

using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Hosting;

namespace VTSMVC.Helpers.Utilities
{
public class BlobStorageVirtualPathProvider : VirtualPathProvider
{
    public override bool FileExists(string virtualPath)
    {
        // Check if the file exists on blob storage 
        string cleanVirtualPath = virtualPath.Replace(@"~/Views/Shared/", "");
        if (BlobExists(cleanVirtualPath))
        {
            return true;
        }
        else
        {
            return Previous.FileExists(virtualPath);
        }
    }

    public override VirtualFile GetFile(string virtualPath)
    {
        //This gets called but only at the points where I don't need it 
        //ie it gets called where where I want Azure 
        string cleanVirtualPath = virtualPath.Replace(@"~/Views/Shared/", "");
        if (BlobExists(cleanVirtualPath))
        {
            return new BlobStorageVirtualFile(virtualPath, this);
        }
        else
        {
            return Previous.GetFile(virtualPath);
        }
    }

    public override System.Web.Caching.CacheDependency GetCacheDependency(string virtualPath, System.Collections.IEnumerable virtualPathDependencies, DateTime utcStart)
    {
        return null;
    }

    private bool BlobExists(string cleanVirtualPath)
    {
        switch(cleanVirtualPath)
        {
            //just doing a simple test for now
            case "_MyStorageViewFile.cshtml":
                return true;
            default:
                return false;
        }
    }
}

public class BlobStorageVirtualFile : VirtualFile
{
    protected readonly BlobStorageVirtualPathProvider parent;

    public BlobStorageVirtualFile(string virtualPath, BlobStorageVirtualPathProvider parentProvider) : base(virtualPath)
    {
        parent = parentProvider;
    }

    public override System.IO.Stream Open()
    {
        //Open Method blah blah blah - 
        //not getting to this point since GetFile doesnt get called!!   
    }
}
}

你解决过这个问题吗?我也遇到过同样的事情,我放弃了解决上述问题的努力,而是用另一种方式解决。你想实现什么?我试图将razor视图存储在数据库中,并仅显示特定路由的视图。因此,site.com/CMS/Index会在数据库表中查找名为“Index”的页面,获取razor代码并显示出来。我认为我的等效方法可能对您有效,也可能对您无效,就是将要存储在数据库中的页面转换为部分,然后根据父页面上函数的结果加载正确的部分。所谓函数,我的意思是你可以在你的html和其他文件下面的页面上定义“@functions”。或者,您可以使用一个switch语句来代替函数,该语句由模型中的值驱动,以加载相应的分部。但这不要求分部仍然是.cshtml文件中的硬编码视图吗?我仍然选择VirtualPathProvider的方式,奇怪的是它在MVC5中不起作用,而是在更早的版本中。
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Hosting;

namespace VTSMVC.Helpers.Utilities
{
public class BlobStorageVirtualPathProvider : VirtualPathProvider
{
    public override bool FileExists(string virtualPath)
    {
        // Check if the file exists on blob storage 
        string cleanVirtualPath = virtualPath.Replace(@"~/Views/Shared/", "");
        if (BlobExists(cleanVirtualPath))
        {
            return true;
        }
        else
        {
            return Previous.FileExists(virtualPath);
        }
    }

    public override VirtualFile GetFile(string virtualPath)
    {
        //This gets called but only at the points where I don't need it 
        //ie it gets called where where I want Azure 
        string cleanVirtualPath = virtualPath.Replace(@"~/Views/Shared/", "");
        if (BlobExists(cleanVirtualPath))
        {
            return new BlobStorageVirtualFile(virtualPath, this);
        }
        else
        {
            return Previous.GetFile(virtualPath);
        }
    }

    public override System.Web.Caching.CacheDependency GetCacheDependency(string virtualPath, System.Collections.IEnumerable virtualPathDependencies, DateTime utcStart)
    {
        return null;
    }

    private bool BlobExists(string cleanVirtualPath)
    {
        switch(cleanVirtualPath)
        {
            //just doing a simple test for now
            case "_MyStorageViewFile.cshtml":
                return true;
            default:
                return false;
        }
    }
}

public class BlobStorageVirtualFile : VirtualFile
{
    protected readonly BlobStorageVirtualPathProvider parent;

    public BlobStorageVirtualFile(string virtualPath, BlobStorageVirtualPathProvider parentProvider) : base(virtualPath)
    {
        parent = parentProvider;
    }

    public override System.IO.Stream Open()
    {
        //Open Method blah blah blah - 
        //not getting to this point since GetFile doesnt get called!!   
    }
}
}