【问题标题】:Call external controller method and render the view调用外部控制器方法并渲染视图
【发布时间】:2015-05-03 04:13:00
【问题描述】:

我正在使用一个名为 MVCForum 的项目,并在解决方案中创建了一个新项目,出于演示目的,我们将其称为“ExternalApp”。

现在,我已将 ExternalApp 引用添加到 MCVForum 应用程序中,并且可以调用控制器:http://mysite[.]com/TestController

“TestController”是我的外部控制器。也就是说,控制器存在于 ExternalApp 中。

问题是当我尝试从TestController中的“Test”返回视图时,找不到该视图。

The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Themes/Metro/Views/Test/Index.cshtml
~/Themes/Metro/Views/Extensions/Test/Index.cshtml
~/Views/Extensions/Test/Index.cshtml
~/Views/Test/Index.cshtml
~/Views/Test/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml

应用程序似乎在它自己的项目中寻找视图,而不是在 ExternalApp/Views 文件夹中。如何让我的外部应用呈现正确的视图?

【问题讨论】:

标签: c# .net asp.net-mvc razor model-view-controller


【解决方案1】:

您可以使用 Razor Generator 之类的东西将您的视图编译到您的 ExternalApp 程序集中,或者您可以在一个站点下分别运行这两个应用程序。

【讨论】:

    【解决方案2】:

    您可以创建自定义视图引擎,但如 here您所述,您需要进行一些修改:

    1. 为了使 MVCExternalApp 项目中的视图在运行时可用,必须将它们复制到 MVCForum 输出文件夹。除非您希望手动执行此操作,否则您必须明确告诉每个视图复制到输出。此选项强制文件进入 bin 文件夹。 对于每个视图,右键单击并选择属性。将“复制到输出目录”选项更改为“始终复制”。这将确保在构建引用项目时始终将文件放入输出中。您还需要为 Web.config 执行此操作。

    2. 创建自定义视图引擎:

      public class CustomViewEngine: RazorViewEngine 
      {
         public CMSViewEngine()
         {               
             ViewLocationFormats = new string[] 
             {
                 "~/Views/{1}/{0}.cshtml",
                 "~/Views/{1}/{0}.vbhtml",
                 "~/Views/Shared/{0}.cshtml",
                 "~/Views/Shared/{0}.vbhtml",
                 "~/bin/Views/{1}/{0}.cshtml", 
                 "~/bin/Views/{1}/{0}.vbhtml", 
                 "~/bin/Views/Shared/{0}.cshtml", 
                 "~/bin/Views/Shared/{0}.vbhtml"
             };
      
             PartialViewLocationFormats = new[]
             {
                 "~/Views/{1}/{0}.cshtml",
                 "~/Views/{1}/{0}.vbhtml",
                 "~/Views/Shared/{0}.cshtml",
                 "~/Views/Shared/{0}.vbhtml",
                 "~/bin/Views/{1}/{0}.cshtml", 
                 "~/bin/Views/{1}/{0}.vbhtml", 
                 "~/bin/Views/Shared/{0}.cshtml", 
                 "~/bin/Views/Shared/{0}.vbhtml"
             };
         }
      }
      

    我只覆盖 PartialViewLocationFormats 和 ViewLocationFormats,但您可以根据需要覆盖其余位置;

    1. 在 Global.asax 的 Application_Start 方法中注册视图引擎:

      protected void Application_Start()
      {
          AreaRegistration.RegisterAllAreas();
          RouteConfig.RegisterRoutes(RouteTable.Routes);
      
          //Remove all view engine
          ViewEngines.Engines.Clear();
      
          //Add Custom view Engine Derived from Razor
          ViewEngines.Engines.Add(new CustomViewEngine());
      }
      

    【讨论】:

    • 由于不可避免的“链接腐烂”,可以通过详细总结链接文章的内容来更好地回答这个问题,因为它与这个问题直接相关。
    猜你喜欢
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-19
    • 1970-01-01
    相关资源
    最近更新 更多