gpt4 book ai didi

c# - 部署后找不到嵌入式 Razor View (IIS)

转载 作者:行者123 更新时间:2023-11-30 13:03:26 24 4
gpt4 key购买 nike

我目前正在从事一个以插件式系统为特色的项目。 Web 项目启动后,它会查找插件 dll 并构建 GUI。 dll 包含嵌入式 View 和嵌入式资源,如图片或 javascript。

  • 我使用 RazorGenerator(VS 插件)从 View 生成 cs 文件, View 的构建操作设置为“嵌入式资源”,自定义工具设置为“RazorGenerator”

  • 我使用自定义虚拟路径提供程序(与此非常相似 one )

  • 我使用我在网上找到的解决方法注册了我的虚拟路径提供程序(这是必须的,因为如果我不使用它,它就不会按预期工作):

        var assemblyResourceProvider = new AssemblyResourceProvider();
    var hostingEnvironmentInstance = (HostingEnvironment)typeof(HostingEnvironment).InvokeMember("_theHostingEnvironment", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.GetField, null, null, null);
    if (hostingEnvironmentInstance == null)
    return;

    var mi = typeof(HostingEnvironment).GetMethod("RegisterVirtualPathProviderInternal", BindingFlags.NonPublic | BindingFlags.Static);
    if (mi == null)
    return;

    mi.Invoke(hostingEnvironmentInstance, new object[] { assemblyResourceProvider });
  • 我在我的 RouteConfig.cs 中使用它来使嵌入式脚本和图片文件工作:

       routes.IgnoreRoute("{*staticfile}", new { staticfile = @".*\.(css|js|gif|png|jpg|exe)(/.*)?" });
  • 我将这些行(还有更多行)添加到我的 web.config 中以告诉 asp 将我的路径提供程序用于我的嵌入式源:

      <add name="AspNetStaticFileHandler-PNG" path="*.png" verb="GET,HEAD" type="System.Web.StaticFileHandler"/>
  • 我使用 Boc.Web.PrecompiledViews dll 来注册 RazorGenerator 生成的路径属性

    BoC.Web.Mvc.PrecompiledViews.ApplicationPartRegistry.Register(assembly);

一切都在 Visual Studio 中按预期工作。嵌入在 dll 中的 View 和资源正在完美地显示和工作。我还使用 IIS 7.5 从 Visual Studio 运行我的 Web 应用程序。所有的可能性都有效:

  • 如果我提供由 Razor Generator 生成的虚拟路径(例如:“~/Views/ViewTest/Test),它会起作用
  • 如果我提供使用我的资源提供程序的 Dll 资源路径(例如:“DllResources/ViewTest.dll/Views/ViewTest/Test.cshtml),它会起作用

但是,在我部署我的 Web 应用程序并使用 IIS 7.5 运行它之后, View (仅 View !)没有显示!如果我使用虚拟路径(选项 1)处理 View ,IIS 会给出此错误消息:

    The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:

如果我使用 DllResources 路径(选项 2)处理 View ,IIS 会给出此错误消息:

    The file '/Views/ViewTest/DllResources/EmbeddedResourceTest/Views/ViewTest/Test.cshtml' has not been pre-compiled, and cannot be requested.

我不知道现在该怎么做才能让它发挥作用。我试图用其他解决方案替换每一步。结果还是一样。在 VS 中完美运行,但部署后 View 未显示。

是否缺少 IIS 的某些配置?我在 web.config 中遗漏了什么吗?请帮我解决这个问题。

谢谢,诺伯特

最佳答案

好的,经过 1 周的痛苦我终于想出了解决方案。问题出在网络项目的发布上。这是我所做的:

我一直在 Microsoft.Net 文件夹中使用 aspnet_compiler 发布我的网络应用程序。这是由成功的 Web 项目构建触发的构建后事件。我删除了此事件,并在 Web 项目的 .csproj 文件中将其替换为“AfterBuild”脚本。我在 this question 上找到了脚本.通过这种新的发布方法,嵌入式 View 终于显示出来了。

希望这对以后的人有帮助。

问候,

关于c# - 部署后找不到嵌入式 Razor View (IIS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12957632/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com