I'm starting to move away from having my classes in a folder called App_Code in my MVC4 projects, and moving all my classes into the folder called Models.
Now I've encountered a problem with my old code.
In my cshtml files I used to call my App_Code classes with a #using projectName.App_Code in the beginning of the file.
But that doesn't work now.
My question is therefore; How would I call a class in my Model folder from my cshtml files?
If you have moved your classes (and changed the namespace when you moved them) then you will need to change the #using namespace reference in the cshtml file from the old reference to the new reference.
To help with this, you could change your web project to compile the views to show any references that fail when you perform a build. To do this you will need to change the <MVCBuildViews> setting in the .csproj file to true as mentioned here by Phil Haack.
Additionally, as mentioned here in the answer by Javad_Amiry, you can update the list of namespaces that are used by your views by updating the <namespaces> setting in the the web.config file.
Related
I have created .net core application some days ago. Now I don't remember exactly which template I have selected for the project. What is the way to know the project template selected for any project.
Problem here is when I generate "Controller with Edit/View using Entity Framework" the default behavior is it should generate controller specific folder in "Views" folder but it is not generating that folder.
If I click on any method (public ActionResult View()) and try to generate view it is generating view in the controller folder only. It should generate this in controller specific folder in "Views" folder.
You can see RouteConfig.cs file is inside App_Start folder.
But it does not have anything that will help to solve your problem.
you can follow my last comment.
The same problem happened with me. I just cut this folder with the generated file and pest it inside the views folder even though it worked. you can also try this
I am building a universal class library with Xaml Views that need to statically reference custom resources (StaticResource) defined inside the same class library.
How can i do that in a designer friendly way without importing the resource file inside every view?
Using VS2013 Update 3. It seems that the designer works only for views inside the specific universal app projects and when the resources are defined inside App.xaml without merging.
In MVC4 you can render CSS stles by using
Styles.Render(path here)
Similarly scripts can be rendered by
Scripts.Render(path here)
But how do render XSL/XSLT stylesheets? Will Styles.Render do the trick?
The reason you do #Styles.Render or #Scripts.Render is because it references a bundle created somewhere in Global.asax, usually in the BundleConfig (look in your App_Start folder). They are created using the code
bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
"~/Content/themes/base/jquery.ui.core.css",
"~/Content/themes/base/jquery.ui.resizable.css"...
so then you can use them in your views as #Styles.Render("~/Content/themes/base/css"). The framework actually creates a virtual file with the path "~/Content/themes/base/css" to enable this bundling technology.
I dont think the stylebundle can be used with xsl/xslt stylesheets, although I dont know for sure. Here are some relevant question to boot.
How to apply an XSLT Stylesheet in C#
or any of these really
https://stackoverflow.com/questions/tagged/xslt+asp.net-mvc
In VS2008, I am adding new classes to a web project.
When I right-click on App-Code -> Add -> New Item -> Class ...
The build action for the newly created item is set to content instead of compile. This seems like it would be a problem with the template. I've found several others through google who have run into this issue, but nobody seemed to have found a more permanent solution, other than "change it from content to compile after creation."
My question: Does anyone know of a fix for this, official or otherwise?
App_Code is a special folder meant for folder based projects.
This is just a hunch, but it might be that you have a project file based project, instead of a simple folder based one.
I am creating a number of WPF applications that all relate around one central WPF application and need to share the same styles and resources. In the first place I created a folder in my main project and added some XAML styles which all worked in displaying UI in the styles I wanted.
Later, I created another application but wanted to share the same styles so I moved the styles into a third project, added that project to the main application and added the reference to the styles project.
The problem is that the will not recognise my styles project. I prefixed the style with the project name and that did not work at all.
I have now got to the point where I can enter the entire relative path to the xaml files in the Path section of the ResourceDictionary and that works. However, If I move the project to another folder then that would fail. I suppose you may ask why I would do that and there are reasons, such as simply refactoring my project structure. Therefore, it would be easier to address the resources by reference rather than file.
I know the answer is inches away but I cannot find anything related to styles from another project.
I have had great feedback from this site so far and any help would be greatly appreciated.
OK, I have sussed it! Basically, you add your styles to a set of resource dictionaries in a separate project then you add the project to the solution and make a reference to it in your main project. I got that far but needed to reference the xaml resource files from my resources in my main project.
I tried using the full path name but while that worked, it was not a very good solution because if I move my project and re-link it, the file will because invalid.
The syntax I used that works is as follows:
<ResourceDictionary Source="/StyleResource;component/MSResources/TabControl.xaml" />
Where StyleResources is the name of my project that holds my shared styles, MSResources is the folder in the styles project that holds the xaml file and the file name at the end is obvious.
I have not yet read up about the “component” reference but it works so I will look at it later and update this post.