Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Nancy.ViewEngines.Razor v0.12.1.0 support Layout views amd _ViewStart?

Tags:

razor

nancy

I'm just getting to grips with Nancy, using the current build on Nuget, v0.12.1.0 and I'd like to use the Razor view engine.

Does v0.12.1.0 of the Razor view engine support the Layout and _ViewStart?

e.g.

I have the following in ~/Views/_ViewStart.cshtml

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

and the following in ~/Views/Shared/_Layout.cshtml

@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<dynamic>
<!DOCTYPE html>
<html>
<head>
    @RenderSection("head", false)
</head>
<body>
    @RenderBody()
</body>
</html>

But all I'm getting when I hit the homepage is the content of a view I've set in ~/Views/Home/Index.cshtml

<h1>Home</h1>

My Home module looks like this:

public class Home : NancyModule
{
    public Home()
        : base("")
    {
        Get["/"] = _ => View["Index"];
    }
}
like image 325
Greg B Avatar asked Oct 15 '12 17:10

Greg B


4 Answers

I wanted to add to the answer because I lost about a day trying to figure this one out:.

Nancy.RequestExecutionException: Oh noes! ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at Nancy.ViewEngines.DefaultViewCache.GetOrAdd[TCompiledView](ViewLocationResult viewLocationResult, Func`2 valueFactory)
   at Nancy.ViewEngines.Razor.RazorViewEngine.GetOrCompileView(ViewLocationResult viewLocationResult, IRenderContext renderContext, Assembly referencingAssembly, Type passedModelType)
   at System.Dynamic.UpdateDelegates.UpdateAndExecute5[T0,T1,T2,T3,T4,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
   at CallSite.Target(Closure , CallSite , RazorViewEngine , ViewLocationResult , IRenderContext , Assembly , Object )
   at Nancy.ViewEngines.Razor.RazorViewEngine.GetViewInstance(ViewLocationResult viewLocationResult, IRenderContext renderContext, Assembly referencingAssembly, Object model)
   at System.Dynamic.UpdateDelegates.UpdateAndExecute5[T0,T1,T2,T3,T4,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
   at Nancy.ViewEngines.Razor.RazorViewEngine.<>c__DisplayClass27.b__26(Stream stream)
   at Nancy.Responses.MaterialisingResponse.PreExecute(NancyContext context)

Similar to Richard Banks's answer - Nancy doesn't resolve the default layout path in your Views/_ViewStart.cshtml file (ie: ~/Views/Shared/_Layout.cshtml). The ~/ isn't interpreted in the view resolver in Nancy.

You can either remove the ~/ part, or comment out the default layout and manually specify a layout on each of your views. The reason I did the latter was because Nancy still executes the _Layout.cshtml even if you've nulled out the layout in your view.

like image 186
Andrew dh Avatar answered Sep 28 '22 07:09

Andrew dh


As of Nancy v0.20 the _ViewStart file is now supported.

As a tip, when referencing your layout file, don't prefix the location wtih ~/. Just have the following or you'll get null reference exceptions.

@{
    Layout = "Views/Shared/_Layout.cshtml";
}
like image 28
Richard Banks Avatar answered Oct 22 '22 20:10

Richard Banks


_ViewStart is not something we (currently) support, but Layout is definitely supported on a per-view basis

like image 8
TheCodeJunkie Avatar answered Oct 22 '22 20:10

TheCodeJunkie


From what I can tell, the simple answer would be no.

A more detailed answer, from the limited research that I did.

In MVC 2, Microsoft added a System.Web.Razor dll that contained the Razor parser and code generator. The _ViewStart feature was added in MVC 3 and in that version, Microsoft didn't create a new System.Web.Razor dll. Rather they embedded the new parser and code generator into the new System.Web.Mvc dll under the System.Web.Mvc.Razor namespace. I'm not sure the reasons behind this change, maybe to simplify deployment.

The Nancy Razor viewengine references the original razor parser and code generator and so won't be able to access any of the new functionality of Razor in MVC 3. I haven't had any discussions with the NancyFx guys and didn't see (after a quick search) any issues on GitHub or discussions in the google group, so I don't know if they are planning to change the viewengine or not.

like image 2
Rich McCollister Avatar answered Oct 22 '22 19:10

Rich McCollister