c# - 无法在自宿主应用程序中配置新程序集

标签 c# razor nancy

我在用 C# 编写的 Windows 窗体应用程序中使用 NancyFX 创建了一个新的自托管 Web 应用程序。我添加了 Razor 支持,并且成功地路由到 Razor View,没问题。我遇到的问题是当我尝试将一些数据绑定(bind)到页面时,将数据表传递给 View 。 Nancy 提示说它不能引用类型 System.ComponentModel.MarshalByValueComponent,因为项目中没有系统程序集。我阅读了很多关于在 app.config 中添加“razor”部分的文章,定义了我需要的组件和命名空间。但是当我尝试这样做时,我在尝试启动主机时遇到另一个错误:

An unhandled exception of type 'Nancy.TinyIoc.TinyIoCResolutionException' occurred in Nancy.dll

Additional information: Unable to resolve type: Nancy.ViewEngines.ViewEngineApplicationStartup

这是我的app.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="razor" type="Nancy.ViewEngines.Razor.RazorConfigurationSection, Nancy.ViewEngines.Razor" />
    </sectionGroup>
  </configSections>
  <appSettings>
    <add key="webPages:Enabled" value="false" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
  </system.web>
  <system.web.webPages.razor>
    <pages pageBaseType="Nancy.ViewEngines.Razor.NancyRazorViewBase">
      <namespaces>
        <add namespace="Nancy.ViewEngines.Razor" />
        <add namespace="System" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

  <razor disableAutoIncludeModelNamespace="false">
    <assemblies>
      <add assembly="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    </assemblies>
    <namespaces>
      <add namespace="System" />
    </namespaces>
  </razor>
</configuration>

你能帮我解决这个问题吗?即使我阅读了所有这些帖子,我也找不到关于使用 Razor 支持创建自托管应用程序的真实示例。

最佳答案

我刚刚在 GitHub 中为您创建了一个存储库。 这是混合自托管和 Razor 的简单解决方案。

检查它是否是您要搜索的内容: NancySelfHostRazorTest

祝你好运。

更新

即使使用 Nancy.Hosting.Aspnet,我也能够重现您的错误。这就是我要解决的问题:

更改您的配置部分添加此行:

<section name="razor" type="Nancy.ViewEngines.Razor.RazorConfigurationSection, Nancy.ViewEngines.Razor" />

并在该部分下方添加:

<razor disableAutoIncludeModelNamespace="false">
    <assemblies>
      <add assembly="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <add assembly="System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    </assemblies>
  </razor>

最后,基于我的 Web.Config,您将以类似这样的方式结束:

  <configSections>
    <section name="razor" type="Nancy.ViewEngines.Razor.RazorConfigurationSection, Nancy.ViewEngines.Razor" />
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <razor disableAutoIncludeModelNamespace="false">
    <assemblies>
      <add assembly="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <add assembly="System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    </assemblies>
  </razor>

在 View 中,尝试一些东西 like this :

@using System.Data; 

@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<System.Data.DataTable>

<h2>Report</h2> 

<table> 
    <thead> 
    <tr> 
    @foreach (DataColumn col in Model.Columns)     
    {          
        <th>@col.ColumnName</th> 
    }     
    </tr> 
    </thead>         
    <tbody> 
    @foreach (DataRow row in Model.Rows)     
    {         
        <tr> 
        @foreach (DataColumn col in Model.Columns)         
        {              
            <td>@row[col.ColumnName]</td> 
        }         
        </tr> 
    }     
    </tbody> 
</table>

Ps: I updated the repository, with a new Module "/data" it does exactly what you want.

关于c# - 无法在自宿主应用程序中配置新程序集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31917570/

相关文章:

Javascript 显示代码而不是执行

c# - 如何将 anchor 标记 HTML 链接转换为使用 @Html.ActionLink

c# - 请求了错误的脚本 url

rest - 南希 : parsing "multipart/form-data" requests

c# - 如何更改查询字符串参数的值?

c# - Aspose.Words - 邮件合并图像

c# - WP7 在您滚动时更新 bing map

c# - WPF:绑定(bind)到 DataGrid 中行的 SelectedItem

c# - 使用 MVC/Razor 传递 C# 变量以与 Dygraph 一起使用

c# - Nancyfx 自托管无法停止 HttpListener