c# - NancyFx - Razor 编译错误

标签 c# asp.net-mvc razor nancy owin

编译 View 时出错

Error compiling template: views/devices.cshtml

Errors:
    [CS0234] Line: 3 Column: 27 - The type or namespace name 'Services' does 
    not exist in the namespace 'Rioxo.Companion' 
    (are you missing an assembly reference?)

Details:
@using System
@using System.Collections.Generic
@using Rioxo.Companion.Services <---

web.config

<razor disableAutoIncludeModelNamespace="false">
    <assemblies>
        <add assembly="Server32" />
        <add assembly="Rioxo.Companion.Services" />
    </assemblies>
    <namespaces>
        <add namespace="Rioxo.Companion.Server" />
        <add namespace="Rioxo.Companion.Services" />
    </namespaces>
</razor>

这可能是什么问题?

最佳答案

编辑: 原来的问题是通过将配置放在正确的 .config 文件中解决的,以供将来引用,在这里添加您也可以实现自己的 IRazorConfiguration Nancy 会自动选择哪个,这意味着您根本不需要任何 .config 注册。

例子:

public class RazorConfig : IRazorConfiguration
{
    public IEnumerable<string> GetAssemblyNames()
    {
        yield return "MyWebsite.Web";
        yield return "MyWebsite.Models";
        yield return "Sandra.SimpleValidator";
        yield return "ServiceStack.Text";
    }

    public IEnumerable<string> GetDefaultNamespaces()
    {
        yield return "Nancy.Validation";
        yield return "System.Globalization";
        yield return "System.Collections.Generic";
        yield return "System.Linq";
        yield return "MyWebsite.Web";
        yield return "MyWebsite.Models";
        yield return "MyWebsite.Web.ViewModels";
        yield return "MyWebsite.Web.Helpers.RazorHelpers";
    }

    public bool AutoIncludeModelNamespace
    {
        get { return true; }
    }
}

原答案:

我其实不知道什么Rioxo是,他们的网站似乎没有下载。

所以我在这里拍摄并假设它是因为您没有正确地按名称引用程序集。

我觉得名字大概是Rioxo.CompanionRioxo

因此更新 <assemblies>部分类似于:

<razor disableAutoIncludeModelNamespace="false">
    <assemblies>
        <add assembly="Server32" />
        <add assembly="Rioxo" />
    </assemblies>
    <namespaces>
        <add namespace="Rioxo.Companion.Server" />
        <add namespace="Rioxo.Companion.Services" />
    </namespaces>
</razor>

<razor disableAutoIncludeModelNamespace="false">
    <assemblies>
        <add assembly="Server32" />
        <add assembly="Rioxo.Companion" />
    </assemblies>
    <namespaces>
        <add namespace="Rioxo.Companion.Server" />
        <add namespace="Rioxo.Companion.Services" />
    </namespaces>
</razor>

应该可以解决问题。

关于c# - NancyFx - Razor 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21877455/

相关文章:

c# - 使用 Azure AD 在 ASP.NET Web 应用程序中进行身份验证

c# - 如何在 visual studio 中访问 SQL 数据库? ( Razor cshtml,C#)

Javascript onclick不起作用,连续点击多个图像

vb.net - VB.Net 中的 CheckBoxFor Nullable Boolean

c# - 无法加载非常大的 JSON(92,000 行)

c# - 从类继承时编译错误

c# - 更新联合表时,父记录被删除

c# - 模型属性作为空值传递给操作

c# - 如何在 Json.NET 中将巨大的 JSON 文件解析为流?

asp.net-mvc - 带有 GET 方法的 Html.BeginForm()