Nancy使用Razor视图引擎:嵌入式视图无法正常工作

时间:2014-03-14 11:41:12

标签: c# razor web nancy

我有以下代码:

public class Program : NancyModule
{
    static void Main(string[] args)
    {
        using (var host = new NancyHost(new Uri("http://localhost:444"), new CustomConventionsBootstrapper()))
        {
            host.Start();
            Console.ReadLine();
        }
    }

    public Program()
    {
        Get["/"] = parameter =>
        {
            dynamic var = new ExpandoObject();
            var.Test = "Lol";
            return View["RazorView.cshtml", var];
        };
    }
}

public class CustomConventionsBootstrapper : DefaultNancyBootstrapper
{
    protected override void ConfigureApplicationContainer(TinyIoCContainer container)
    {
        base.ConfigureApplicationContainer(container);
        //This should be the assembly your views are embedded in

        var assembly = Assembly.GetEntryAssembly();

        ResourceViewLocationProvider.RootNamespaces.Add(assembly, "NancyTest.Views");


    }

    protected override NancyInternalConfiguration InternalConfiguration
    {
        get
        {
            var res = base.InternalConfiguration;
            res.ViewLocationProvider = typeof(ResourceViewLocationProvider);
            return res;
        }
    }

    void OnConfigurationBuilder(NancyInternalConfiguration x)
    {
        x.ViewLocationProvider = typeof(ResourceViewLocationProvider);
    }

}

我在一个文件夹中有RazorView.cshtml我的项目中的视图设置为嵌入式资源,但每次打开页面都会给我

   Nancy.RequestExecutionException: Oh noes! ---> Nancy.ViewEngines.ViewNotFoundException: Unable to locate view 'RazorView.cshtml'
Currently available view engine extensions: sshtml,html,htm,cshtml,vbhtml
Locations inspected: views/Program/RazorView.cshtml-en-GB,views/Program/RazorView.cshtml,Program/RazorView.cshtml-en-GB,Program/RazorView.cshtml,views/RazorView.cshtml-en-GB,views/RazorView.cshtml,RazorView.cshtml-en-GB,RazorView.cshtml
Root path: C:\Users\Student\documents\visual studio 2013\Projects\NancyTest\NancyTest\bin\Debug
If you were expecting raw data back, make sure you set the 'Accept'-header of the request to correct format, for example 'application/json'
   bij Nancy.ViewEngines.DefaultViewFactory.GetRenderedView(String viewName, Object model, ViewLocationContext viewLocationContext)
   bij Nancy.ViewEngines.DefaultViewFactory.RenderView(String viewName, Object model, ViewLocationContext viewLocationContext)
   bij Nancy.Responses.Negotiation.ViewProcessor.Process(MediaRange requestedMediaRange, Object model, NancyContext context)
   bij Nancy.Routing.DefaultRouteInvoker.NegotiateResponse(IEnumerable`1 compatibleHeaders, Object model, Negotiator negotiator, NancyContext context)
   bij Nancy.Routing.DefaultRouteInvoker.ProcessAsNegotiator(Object routeResult, NancyContext context)
   bij Nancy.Routing.DefaultRouteInvoker.InvokeRouteWithStrategy(Object result, NancyContext context)
   bij Nancy.Routing.DefaultRouteInvoker.<>c__DisplayClass9.b__5(Task`1 completedTask)
   --- Einde van intern uitzonderingsstackpad ---
   bij Nancy.NancyEngine.InvokeOnErrorHook(NancyContext context, ErrorPipeline pipeline, Exception ex)

值得注意的是,只有在使用Razor视图时才会发生这种情况。当我使用一个简单的html文件时,它可以很好地找到这些文件。我甚至试图在当前(0.22.2)Nancy版本上运行在线发现的旧示例项目,但也没有运气。发生了什么事?

由于

2 个答案:

答案 0 :(得分:2)

好的想出来了,但我不得不说这有点愚蠢。检查来源,这是罪魁祸首:

ResourceAssemblyProvider.cs第31行

private static IEnumerable<Assembly> GetFilteredAssemblies()
{
    return AppDomainAssemblyTypeScanner.Assemblies
       .Where(x => !x.IsDynamic)
       .Where(x => !x.GetName().Name.StartsWith("Nancy", StringComparison.OrdinalIgnoreCase));
}

我的集会被称为NancyTest&#39;。改为&#39; TestNancy&#39;,现在有效。我建议找一个更好的方法来排除你自己的集会。

答案 1 :(得分:0)

此外,在使用嵌入式视图时,人们很容易忘记将视图添加为嵌入式资源(这是我需要一段时间才能习惯的常见问题):

右键单击view-file - &gt;属性 - &gt;构建行动 - &gt;选择“嵌入资源”