加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 来自其他MVC项目的MVC4托管视图

发布时间:2020-12-16 09:38:53 所属栏目:asp.Net 来源:网络整理
导读:我试图弄清楚如何托管在不同解决方案下构建的MVC4应用程序.有许多使用带有区域的RazorGenerator nuget包在SO和Web上执行此操作的示例 – 它非常有效.在我的情况下,我想避免使用区域 – 我公司开发的每个应用程序都将在它自己的MVC4项目中(然后集中在同一个解
我试图弄清楚如何托管在不同解决方案下构建的MVC4应用程序.有许多使用带有区域的RazorGenerator nuget包在SO和Web上执行此操作的示例 – 它非常有效.在我的情况下,我想避免使用区域 – 我公司开发的每个应用程序都将在它自己的MVC4项目中(然后集中在同一个解决方案中).

我已将RazorGenerator集成到我的应用程序中,代码生成正如预期的那样.但是,我的主机解决方案无法在其默认位置找到View.例如,我在一个名为MyAccount / Index的应用程序中内置了一个Controller / View.

控制器:

namespace Accounts.Controllers
{
    public class MyAccountController : Controller
    {
        //
        // GET: /MyAccount/
        public ActionResult Index()
        {
            return View();
        }
    }
}

视图(从RazorGenerator生成):

namespace Accounts.Views.MyAccount
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Web;
    using System.Web.Helpers;
    using System.Web.Mvc;
    using System.Web.Mvc.Ajax;
    using System.Web.Mvc.Html;
    using System.Web.Routing;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.WebPages;

    [System.CodeDom.Compiler.GeneratedCodeAttribute("RazorGenerator","1.5.4.0")]
    [System.Web.WebPages.PageVirtualPathAttribute("~/Views/MyAccount/Index.cshtml")]
    public partial class Index : System.Web.Mvc.WebViewPage<dynamic>
    {
        public Index()
        {
        }
        public override void Execute()
        {

            #line 1 "....ViewsMyAccountIndex.cshtml"

    ViewBag.Title = "Index";


            #line default
            #line hidden
WriteLiteral("rnrn<h2>Index</h2>rnrnMy AccountController Index view.");

        }
    }
}

我知道默认情况下,ViewEngines会尝试在默认位置(Views和Shared)中查找此视图,因此我将自己的ViewEngine添加到Engines集合中:

MyViewEngine.cs:

public class MyViewEngine : RazorViewEngine
    {
        private static string[] _viewLocations 
           = new string[]
             {
                "~/Accounts/Views/{1}/{0}.cshtml"
             };

        public MyViewEngine()
        {
            base.ViewLocationFormats = ViewLocationFormats.Union(_viewLocations).ToArray();
        }
    }

但是,仍未找到该视图:

The view 'Index' or its master was not found or no view engine supports the searched locations. 
The following locations were searched: 
~/Views/MyAccount/Index.cshtml
~/Views/Shared/Index.cshtml
~/Views/MyAccount/Index.aspx
~/Views/MyAccount/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/MyAccount/Index.vbhtml 
~/Views/Shared/Index.vbhtml
~/Accounts/Views/MyAccount/Index.cshtml

也许我误解了视图的位置 – 我以为它会在Accounts / Views / MyAccount /中找到.我有什么想法可能做错了吗?

谢谢!

解决方法

发现我的问题 – 这是由于没有使用RazorGeneratorMvcStart预热代码.添加nuget包时,它会自动生成到App_Start文件夹中,但是我错误地删除了它.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读