无法使用Razor View Engine访问来自其他项目的视图 [英] Can't access views from different project using Razor View Engine

查看:85
本文介绍了无法使用Razor View Engine访问来自其他项目的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个用例中,我有许多Web项目都使用相同的管理通用网页,因此我开始将视图隔离到一个单独的项目中,所有解决方案都可以引用和使用.

I have a use case where I have many web projects that all use the same management general-purpose web pages, so I started to isolate my views to a separate project where all solutions can reference and use.

由于当前,每当有新更改时,我都必须在解决方案中复制并粘贴视图文件.

Because currently, I have to copy-paste the view files across solutions whenever I have a new change.

基于此 answer ,我添加了一个新的自定义视图引擎,然后将路径添加到了我的位置现场直播(在我的主项目旁边的一个单独项目中).

Based on this answer, I added a new custom view engine, and then added the path to the location where my views live (in a separate project beside my main project).

    public class CustomViewEngine : RazorViewEngine
    {
        public CustomViewEngine()
        {
            MasterLocationFormats = new string[]
            {
        "~/Views/{1}/{0}.aspx",
        "~/Views/{1}/{0}.ascx",
        "~/Views/Shared/{0}.aspx",
        "~/Views/Shared/{0}.ascx",
        "~/Shared.Views/Views/{1}/{0}.aspx",
        "~/Shared.Views/Views/{1}/{0}.ascx",

            };
            ViewLocationFormats = new string[]
            {
        "~/Views/{1}/{0}.aspx",
        "~/Views/{1}/{0}.ascx",
        "~/Views/Shared/{0}.aspx",
        "~/Views/Shared/{0}.ascx",
        "~/../Shared.Views/Views/{1}/{0}.aspx",
        "~/../Shared.Views/Views/{1}/{0}.ascx",
            };
      }
    }

包括以下内容时:

"~/../Shared.Views/Views/{1}/{0}.aspx",
"~/../Shared.Views/Views/{1}/{0}.ascx",

我遇到以下异常:

不能使用前导..退出顶层目录.描述:当前执行过程中发生未处理的异常网络请求.请查看堆栈跟踪以获取有关以下内容的更多信息错误及其在代码中的起源.

Cannot use a leading .. to exit above the top directory. 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.

异常详细信息:System.Web.HttpException:无法使用前导..退出顶部目录.

Exception Details: System.Web.HttpException: Cannot use a leading .. to exit above the top directory.

我的文件结构.

Solution
   Shared.Views
      Views
         Profiles
            create.cshtml
            delete.cshtml
            edit.cshtml
            details.cshtml
   MainProject
      Global.asax

要告诉MVC在单独的项目中查找视图,我必须添加正确的路径是什么?

What is the correct path I have to add to tell MVC to look for views in my separate project?

推荐答案

您无法像在做的那样引用另一个项目中的文件.查看

You can’t reference files in another project like you’re doing. Check out Razor Class Libraries if you’re using ASPNET Core 2.1+

这篇关于无法使用Razor View Engine访问来自其他项目的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆