Nancy icon indicating copy to clipboard operation
Nancy copied to clipboard

How can I set Nancy Razor to default when I run a Console App which use Nancy in another project?

Open BrightstarKing opened this issue 6 years ago • 1 comments

I build a C# framwork 4.6.1 Console App, it didn't invoke any Nancy nuget or dll, but invoked another project "NancyHttpCommunicationModule" which use Nancy.Hosting.Self and Nancy.ViewEngines.Razor, and it will return view. The view ends with .html or .sshtml is fine, but .cshtml is not working, and returns the bug: Nancy.RequestExecutionException: Oh noes! ---> Nancy.ViewEngines.ViewNotFoundException: Unable to locate view 'customIndex.cshtml' Currently available view engine extensions: sshtml,html,htm Locations inspected: views/Request/customIndex.cshtml-zh-CN,views/Request/customIndex.cshtml,...... I don't want invoke Nancy.ViewEngines.Razor in my Console App, how can I make it work?

BrightstarKing avatar Oct 24 '18 06:10 BrightstarKing

I'm no expert at this, but I believe what's going on is, one of the things that the NuGet package does when you install Nancy.ViewEngines.Razor is, it adds this to your Web.config (or App.config) file, in the <system.web> section:

    <compilation debug="true" targetFramework="4.7.1">
      <buildProviders>
        <add extension=".cshtml" type="Nancy.ViewEngines.Razor.BuildProviders.NancyCSharpRazorBuildProvider, Nancy.ViewEngines.Razor.BuildProviders" />
        <add extension=".vbhtml" type="Nancy.ViewEngines.Razor.BuildProviders.NancyVisualBasicRazorBuildProvider, Nancy.ViewEngines.Razor.BuildProviders" />
      </buildProviders>
    </compilation>

I think these are the entries that say, "If you're serving a .cshtml file, this is the class that provides it." (NB: You can set debug and targetFramework to whatever is appropriate for your project.)

I don't know if there's a way to programmatically add these entries. You might need to add them to your console application's App.config file. As long as the reference to that other project brings in Nancy.ViewEngines.Razor.BuildProviders.dll by its indirect reference, I think it should work? :-)

Well, that's what I'd try, anyway. :-)

logiclrd avatar Nov 07 '18 16:11 logiclrd