Show missing PartialView as a build error - c#

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

Related

Why does changing the Output Path of my ASP.NET MVC project cause the "Could not load type MvcApplication" error?

I experienced the same problem as this popular question where I would receive the following error when my ASP.NET MVC project's Output Path was set to anything but \bin:
Could not load type 'MvcApplication'
My question is, why does changing the Output Path fix this error? I have build configurations for each environment I deploy to and I would like to keep the builds segregated (DEV builds to \bin\Debug, QA builds to \bin\QA, etc). Is there any way to fix this so I can keep separate build Output Paths but not get this error?
This issue has been happening since at least 2009 (based on when that question was asked), which leads me to believe it might be an intentional design decision rather than a bug...
in VS the bin directory's sub folders (i.e., Build, Debug..QA whatever) are defined by VS itself. You shouldn't need to manually edit the config file.
If you're using VS2017 you can go to Build->Configuration Manager and then add a new "Active Solution Configuration" (click on the drop down and select new)

Controller method is called but there is no code in the controller

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.

How to run the code generated by Altova Mapforce in Visual Studio?

I am using MapForce for the first time. I was able to successfully create an XML mapping. However, I am not sure how to run the generated code in Visual Studio, so that I can use it and see the output. I generated the code for the mapping in C# and did try to run the "Mapping" Project in VS.
Unfortunately, I am not able to due to few errors. There isn't a good documentation on the folders that are created in the project. If I can get a documentation link that I can refer to or if somebody can define why those folders are there and how can I use them/ how I should be running those projects.
Please correct me if I am wrong anywhere.
If you generated code to C:\MyFolder\, then you need to open in Visual Studio the solution C:\MyFolder\Mapping\Mapping.sln. Note that the "Mapping" is the default name, if you configured it to be something else from MapForce settings, then it may be different.
Solutions in other generated folders are supporting libraries, and you should never edit them (or at least do so at your own risk). The only file you can (but you don't normally need to) modify is called MappingConsole.cs. For example, you may want to change the paths of the mapping input/output files. In any case, after you build and run the solution, a Mapping.exe application is created in the Bin\Debug folder.
It would have been helpful if you indicated which errors you get. Perhaps the paths to the mapping input files cannot be resolved relative to the Mapping.exe application?

Using Razor View Engine with Nancy, .cshtml not an available extension

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.

Visual Studio 2013 only builds HTML changes, not C# changes

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.

Categories