I’m working on a project using Nancy on a Mac, and I’m editing my project using a standard text editor (Atom). I would like to use the Razor View Engine, and I’m trying to figure out how.
I included
Nancy.Viewengines.Razor": "1.3.0" in the dependencies in my
project.json file
using Nancy.ViewEngines.Razor; at the top of my HomeModule.cs
file
#inherits
Nancy.ViewEngines.Razor.NancyRazorViewBase<nancytest.Objects.Task>
at the top of the View I would like to use Razor in
(task_added.cshtml), which has an extension of .cshtml
But when I load up the project, I get the error:
Nancy.RequestExecutionException: Oh noes! ---> Nancy.ViewEngines.ViewNotFoundException: Unable to locate view 'task_added.cshtml'
Currently available view engine extensions: sshtml,html,htm```
Is there anything else I should include for it to recognize .cshtml?
Got it to work! Unfortunately, the view engine is built on code that will not run on a Mac. This solution only works on Windows.
I added the following code to my configuration file (Startup.cs), translated from the Razor Engine Wiki.
public class RazorConfig : IRazorConfiguration
{
public IEnumerable<string> GetAssemblyNames()
{
return null;
}
public IEnumerable<string> GetDefaultNamespaces()
{
return null;
}
public bool AutoIncludeModelNamespace
{
get { return false; }
}
}
The line #inherits
Nancy.ViewEngines.Razor.NancyRazorViewBase<nancytest.Objects.Task> at the top of each View is also unnecessary.
Have you read: the Razor Engine Wiki and View location conventions?
Do you have an opportunity to try out your code on Bootcamp, or a Windows machine to see if it's Mac-specific? Presumably you're running on Mono as it's on a Mac, likewise have you got any other projects/ tutorials running on your Mac?
Can you provide more detail on what you have/ haven't tried? Whether you have the tutorial examples working?
At a guess, I'd say your view files (.cshtml) are not somewhere where Nancy is looking for them. What's your folder structure?
Edit
Sorry, I've read it again and obviously Nancy isn't picking up the razor engine (as it explicitly says so). You don't need the using statement in the HomeModule, is's not referenced there.
Have you double-checked the project reference for Nancy and checked that it's been copied into the bin folder?
Enable the diagnostics (Diags) and look in the 'Information' panel, under 'Loaded View Engine'- this will confirm which view engines are loaded, though it will doubtless say the same thing.
Given that Nancy auto-locates view engines by scanning the loaded appdomain and private bin directory for any dlls that reference nancy and auto-registers the IViewEngine types, it strongly suggests that the Razor assembly isn't in the bin folder, or something similar.
I have had the same problem. I am using Nancy self host on a top shelf windows service. My problem was I only has a reference to the core application, but did not have reference to the Nancy.ViewEngines.Razor.dll in my windows service project.
To fix it, I added the Nancy.ViewEngines.Razor.dll refence to my WindowsService project
Here's my story and how I fixed it, in the event that someone has the same issue:
I compiled my Nancy app and it worked fine on my development computer (Windows 10).
I sent the app to another computer via Google Drive, and my client observed the same error as the original poster here on their computer (also Windows 10).
The cause of the issue was Windows blocking the file Nancy.ViewEngines.Razor.dll on my client's computer, because they had downloaded my code from the Internet.
On the client computer, I unblocked the file from the file properties context menu, and the issue was fixed.
P.S. I also unblocked the cshtml files first, but that did not fix the issue, at least by itself.
Related
I have now build a webpage for some API stuff- I now want to use the index.cshtml file (which is build into the structure) - but when I open the index.cshtml file or the Layout.cshtml or the Error.cshtml file in the Visual Studio - I get the exception message as:
CS1980 C# Cannot define a class or member that utilizes 'dynamic' because the compiler required type 'System.Runtime.CompilerServices.DynamicAttribute' cannot be found. Are you missing a reference?
For some time ago, the Azure was installed in my project by a mistake - but I uninstalled it (maybe that was the reason).
I have deleted the View folder (where the index file was included) and added the it again, like on this video.
So here is a problem, during past few months I have been working on my Masters Project and haven't used source control. The project does big data calculation and requires a lot of RAM, therefore sometimes a laptop works really slow or stops responding and then I have to reboot it.
I wrote all the code in controller of my MVC Project. After my last force reboot, my controller has all nulls and does not show any previous code that I have written. In the same way, my app works fine and controller methods are revoked properly.
When I click on controller VS opens an empty notepad document instead of controller code. Unfortunately, I haven't used source control and don't have a build. Any help is highly appreciated.
Here are the files that I have in by debug folder:
In order not to face this kind of situation in future, please back up your data or use source control. If you were not careful like me and did the same mistake, then here is the solution:
Go to your project folder
Go to /YourProject/YourProject.Web/obj/Debug
Find YourProject.Web.dll
Go to https://jetbrains.com/decompilerand download .dll decompiler
Install decompiler and restore .dll to a project
Go to restored project folder->Controllers->RestoredController.cs
Once you open RestoredController.cs you will find the code you lost.
I'm writing a small website using ASP.NET, writing the C# into the .aspx file directly without using code-behind or compiling a DLL. I do this mainly to allow small edits with just a text editor without having to keep going back to Visual Studio.
This is working great, except I want to write a class for some code that will be used by multiple .aspx pages. Is there a way I can add a .cs file in the same way I have C# code in .aspx files?
Good question.
I assume you have a "Web Site" in Visual Studio. If not then perhaps you should invest the time to create a new Web Site and migrate the pages into it. Then publish it and that will make things easier. The problem you describe here will be solved plus other things. If you modify the web site outside of Visual Studio however then that would probably be a problem too. I am a beginner but you probably can open the site live for editing directly. I am not sure.
I was curious about how to do what you are asking so I explored.
Anyway, assuming you already have a Web Site in Visual Studio, use "WebSite" | "Add New Item" to add a class. VS will ask you if you want to create an APP_CODE, say yes. The cs file won't have a namespace, the class will be in the global namespace.
When I got that far I was able to use VS to test a page using the class. When I copied the source, IIS said it could not find the class. So be sure that the APP_CODE directory is in the root of the web site. That probably is not enough so we will continue.
I published the web site. The published web site worked, it found the class. I could not figure out what the difference is between the published site and what I copied over. So I studied the output of the publish and I see it is creating virtual directories and adding ACLs.
I think this is the trick. First be sure the APP_CODE directory is in the root of the web site. Then go into the IIS Manager and find the web site. Right-click and select "Convert to application". When I did that the files that I just copied worked.
If you publish the site using Visual Studio then it will do that for you. You only need to do it once.
I tried a few other things so there might be something else I did that is necessary but I do not think so.
Add something like the following at the top of your .aspx file -
<%# Import namespace = "mydll" %>
This will ensure that the class is accessible in the mark-up, assuming the methods and properties etc are setup to be so.
Any changes to the dll will still require you to edit and build in VS however.
I am building a personal website using asp.NET's webforms in visual studio 2013 express for web and am following this tutorial:
http://www.asp.net/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/introduction-and-overview
My page is structured exactly the same as in the website, I have changed some minor stuff to make it my own but the structure in terms of the C# classes and how the interact with the HTML are exactly the same.
I got to section 5 of the tutorial "Display Data Items and Details" and everything was working fine. I've used git a lot in the past so I decided to create a repository for this project so I can access it at work if I feel like.
Suddenly now when I make changes to the C# classes it won't build. It's even stranger because I if I make a change on an HTML file the change is built. In section 3 of the tutorial we learnt how to make the 'product' classes which are displayed on the products page. If I want to change one of the product names for example, when I build the change is not there. Simultaneously I went and changed some info in the HTML for the contact page, IT CHANGES when I build. Why wont the C# changes take effect when I build any more?
I am relatively new to both asp.NET and visual studio. The HTML changes when I build and the C# does not. When I change either I can see in solution explorer that there is a red tick for pending changes. Why would only the HTML pending change be included in the build and not the C#? How do I ensure that the build is actually building the version I see in my editor window?
EDIT
I do not know if I found the original cause but I found a solution/workaround. I realized that the classes mentioned above were grabbed by the html page from the page's database. The .mdf file for the solution was not being rebuilt whenever I cleaned and built so I physically deleted it and rebuilt the solution and voilla my C# changes occurred. I am still fairly new to this whole thing, can someone explain what the .mdf file does and why it wasn't being rebuilt?
Check your .cs files properties on the properties window in visual studio to make sure their Build Action is set to "Compile", Things that are not set to "Compile" do not get compiled. How MSBuild treats project items depends entirely on their build action. CS files default to "Compile" when you make them, but if you changed them yourself that would be why it doesn't update. Also CS files placed in the App_Start folder default to "Content" and they are compiled by ASP.Net when the Application Starts, so if you changed something in App_Start you need to reset the site.
Not exactly sure what is the problem, but I would do a right mouse click on the solution in Solution Explorer->Clean Solution, then do another build and see if that helps.
I've been reorganising our views at work and moving them around to more appropriate locations.
Because we're currently organising many of our views into their own folders within the Views folder, we're having to reference our views using the full path in RenderView.
Is there a way that I can get Visual Studio to throw a build error if the location of the ascx file that the RenderPartial method points to doesn't exist?
There is an error shown at run time however I'd like it to be part of the build process.
Thanks for any advice
View locations are resolved only at runtime so I really don't see how this could be done, unless you write some custom step in MSBuild that will analyze the source code and check the existence of corresponding view files but this will be a challenging task.
MvcBuildViews might help with this:
Compile Views in ASP.NET MVC