【问题标题】:_ViewStart.cshtml not found to render embedded cshtml_ViewStart.cshtml 未找到呈现嵌入式 cshtml
【发布时间】:2018-08-04 05:04:27
【问题描述】:

我正在尝试从 dll (Asp.Net MVC 5) 加载 MVC 视图。

设置 我有一个 MVC Web 应用程序项目和一个类库项目 (Custom.Views),我有 /CustomViews/Views/MyView/CustomView1.cshtml

CustomView1.cshtml 是一个嵌入式资源,

@inherits System.Web.Mvc.WebViewPage

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


<div>
    Helloooo Custom view in action!!!!!
</div>

在我的 Custom.Views 项目中,我有一个名为 MyView 控制器的控制器,

public ActionResult Shop()
    {
        return View("~/ClientView/Custom.Views.DLL/Custom.Views.CustomViews.Views.MyView.CustomView1.cshtml");
    }

它返回指向嵌入资源的虚拟视图路径。我配置了路由配置来检查这个命名空间“Custom.Views.CustomViews..”控制器。

我实现了一个VirtualPathProvider

public class AssemblyResourceProvider : System.Web.Hosting.VirtualPathProvider
{
    public AssemblyResourceProvider() { }

    private bool IsAppResourcePath(string virtualPath)
    {
        String checkPath = VirtualPathUtility.ToAppRelative(virtualPath);
        return checkPath.StartsWith("~/ClientView/", StringComparison.InvariantCultureIgnoreCase);
    }

    public override bool FileExists(string virtualPath)
    {
        return (IsAppResourcePath(virtualPath) ||
                base.FileExists(virtualPath));
    }

    public override VirtualFile GetFile(string virtualPath)
    {
        if (IsAppResourcePath(virtualPath))
            return new AssemblyResourceVirtualFile(virtualPath);
        else
            return base.GetFile(virtualPath);
    }

    public override CacheDependency GetCacheDependency(string virtualPath,
        IEnumerable virtualPathDependencies, DateTime utcStart)
    {
        if (IsAppResourcePath(virtualPath))
            return null;
        else
            return base.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
    }
}

class AssemblyResourceVirtualFile : VirtualFile
{
    string path;

    public AssemblyResourceVirtualFile(string virtualPath)
        : base(virtualPath)
    {
        path = VirtualPathUtility.ToAppRelative(virtualPath);
    }

    public override System.IO.Stream Open()
    {
        string[] parts = path.Split('/');
        string assemblyName = parts[2];
        string resourceName = parts[3];

        assemblyName = Path.Combine(HttpRuntime.BinDirectory, assemblyName);
        var assembly = Assembly.LoadFile(assemblyName);

        if (assembly != null)
        {
            return assembly.GetManifestResourceStream(resourceName);
        }
        return null;
    }
}

问题

问题 1. 当我输入 url "httlp://localhost:1234/myview/shop" 时,它会点击虚拟路径并返回找到文件流。那部分工作正常。但不久之后,虚拟路径提供程序收到另一个请求,在“~/ClientView/Custom.Views.DLL/_ViewStart.cshtml”中寻找 _ViewStart.cshtml。为什么会这样? 为什么它在该路径中寻找 _ViewStart.cshtml。

【问题讨论】:

标签: asp.net-mvc razor-pages


【解决方案1】:

您可以尝试在_ViewStart.cshtml中定义布局页面:

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

如果多个View使用不同的布局页面,也可以为每个View定义对应的布局页面,如下图:

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

希望这会有所帮助...

【讨论】:

    猜你喜欢
    • 2016-10-24
    • 1970-01-01
    • 2015-03-09
    • 1970-01-01
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多