I am currently working with a solution that contains a C# project and a Razor web page project. I am looking to import a namespace from that C# project to the web page but it gives an error saying that the namespace could not be found. The namespaces in the project are structured "CompanyName.WordThatDescribesNamespace". When I say #using Company.Namespace it says that there is no namespace called Company. Is using that period preventing it from importing the namespace or is there a way around it?
From your question I understood this following this.
You have two projects in a solution
One is a class project (dll) and another one is an MVC project.
You need to access the methods in class project to the MVC.
Please make sure you did the following things
1. Added reference to the dll of the class project having CompanyName.WordThatDescribesNamespace. You can got to Solution Explorer and verify this.
2. If referred , please check the properties, whether you set CopyLocal=True. Deletes the dlls in Bin folder and try to refer and changing it to False and build the project.
3. You can also add namespace to the Web.config residing in the Views folder, so it will be available for all views.
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
...
<add namespace="Infragistics.Web.Mvc" />
</namespaces>
</pages>
Hope any one of the above helps you.
Related
I have an application in which I am using Kendo.Mvc.UI now I am creating another project in which I want to use Kendo.Mvc.UI so I added Kendo.Mvc.UI my bin folder see below Image for reference
and added the below code my main web.config in Pages > Namespaces
<add namespace="Kendo.Mvc.UI" />//the same code is working in my previous project.
and the below line in my web.config of which is in views folder
<add namespace="Kendo.Mvc.UI" /> //this is same in both the web.config
but I am Getting the below error
Compilation Error Description: An error occurred during the
compilation of a resource required to service this request. Please
review the following specific error details and modify your source
code appropriately.
Compiler Error Message: CS0246: The type or namespace name 'kendo'
could not be found (are you missing a using directive or an assembly
reference?)
Source Error:
Line 56:<add namespace="System.Web.Routing" />
Line 57:<add namespace="System.Web.WebPages" />
Line 58:<add namespace="kendo.mvc.ui" />
Line 59:</namespaces>
Line 60:</pages>
Using a project I currently have the following should work: in the main web.config
The directive should be here:
<system.web>
...All other options removed for brevity....
<pages>
<namespaces>
<add namespace="Kendo.Mvc.UI" />
</namespaces>
</pages>
</system.web>
then in the web.config under your Views folder it should look like this:
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization" />
<add namespace="System.Web.Routing" />
<add namespace="Kendo.Mvc.UI" />
</namespaces>
</pages>
</system.web.webPages.razor>
Which based on your first image looks correct.
Assuming you have the web.config files configured correctly then intellisense should kick in after a shutdown and reopen of the project (if it doesn't recognise it straight away) when you type something like #Html.Kendo().<control here>
Assuming you have added the reference to the Kendo.Mvc and Kendo.Mvc.Export dll's into the project.
Failing this working have you tried to use the Telerik Kendo UI for MVC plugin for Visual Studio to upgrade the project as this may also help resolve this issue for you. If you don't have this option this try downloading it either package updater or via the progress updater software for Telerik.
As a side note I noticed in your image that you have a file System.Web.Http.Cors.xml which is in errored state. This may be preventing your project from building correctly which could be impacting the dll for kendo from being loaded correctly.
Personally when adding/referencing dll's in a project I prefer to add the files into a lib folder that sits alongside the project rather than adding the files into a bin folder as the bin folder is usually excluded from source control check-ins.
I have a MVC5 project I am trying to solve some issues with where I am not getting intellisense on the System.Web.MVC components
In the Web.Config in my views folder I have the following;
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Configuration" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization"/>
<add namespace="System.Web.Routing" />
<add namespace="Helpers" />
<add namespace="Common.Net.Mvc" />
<add namespace="Common" />
<add namespace="MvcSiteMapProvider.Web.Html" />
<add namespace="MvcSiteMapProvider.Web.Html.Models" />
</namespaces>
</pages>
</system.web.webPages.razor>
However when I then reference for example Url., ViewBag or Html.ActionLink etc(also if I reference my Common class which is a .resx generated file). I get errors saying they do not existing the current context.
I can compile and run the code successfully and the pages render just fine, it would be nice to eliminate these errors though and get the intellisense I usually get.
I am running VS2017 Community and I have the Razor extension installed and updated. I have intellisense on other projects it just seems to be this one.
Ok, After much searching and wasting of time, this answer here fixed the issue;
The name 'ViewBag' does not exist in the current context - Visual Studio 2015
Here is a run down of the steps I took (the removal and deletion of files may not be required);
Delete the .suo and .csproj.user from the solution having the issues.
Delete the hidden .vs folder form the route of the solution
Delete the temp asp net files from
C:\Users\your.name.here\AppData\Local\Temp\Temporary ASP.NET Files
Delete the files fro here;
C:\Users\your.name.here\AppData\Local\Microsoft\VisualStudio\14.0\ComponentModelCache
Note the above alone didn't fix so then I applied the following in conjunction witht he above.
Compared the .csproj version info with that of the project with the issue and ammend (as it was indeed a project carried over form VS 2012) and as per the link above.
Right click the solution -> 'Manage Nuget Packages...' -> Updates -> select all -> apply updates
This rebuilt the nuget references and the intellisense came back. Hopefully one day, somewhere this will help.
I can't get intellisense for my own html helpers. My CustomHtmlHelpers.cs looks like this:
using System.Web.Mvc;
using System.Text;
using System.Web;
namespace laget.Web.Helpers
{
public static class CustomHtmlHelpers
{
//MY HELPERS
}
}
and in my Web.config:
<pages>
<namespaces>
<add namespace="laget.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages"/>
<add namespace="System.Web.Helpers" />
</namespaces>
</pages>
If I put <#using laget.Web.Helpers> in my view, I get the intellisense issue fixed.
Should it not be enough with the code in Web.config?
Sometimes it doesn't seem to work right away. Try closing the .cshtml file, and re-opening it. Then if that doesn't work, try restarting Visual Studio. Also make sure you actually compiled your project, intellisense won't work with non-compiled helpers.
I'm pretty sure that you're not editing the correct Web.config file.
You need to add your namespace to the one in your Views directory.
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="laget.Web.Helpers" />
</namespaces>
</pages>
</system.web.webPages.razor>
You actually don't need to restart Visual Studio in most cases. All you need to do is close the .cshtml file and reopen it!
It needs it on the local page. I'm pretty sure this has to do with Namespace resolution. It isn't exactly sure what you are referring to without the local using statement.
I ran into this today as well. Sometimes just closing the Razor view's window in Visual Studio and re-opening it will do the trick without having to do a full Visual Studio restart.
I tried to solve an issue like this one yesterday. I had e pre-compiled dll (project name ie: MyHtmlHelpers) containing helpers and lot of other classes.
I had the assembly referenced in the web project and the all "standard"-helpers showed up in intellisense but, even though I added the namespace to both web.config in the root and in the views-folder nothing worked. When running the project helpers works, but not in intellisense.
I added a new class and wrote a new html helper inside the web project, added the namespace to web.config. And that worked.
After some hours add tried my last card, adding the MyHtmlHelpers-project to the same solution as my webproject. That did the trick. I diden't change anything in the configs just added the project to the same solution and changed the reference to point at the project insted of the compiled dll.
Isen't that strange? A VS-bug?
I found that i was adding the reference to the wrong web.config. It's not the main config but the web.config in the views directory...
So now I will show you the steps
1.Create or open an existing class library project (if you open an existing one be sure to remove the MVC5 nuget package)
2.Add the MVC (5.0) nuget package (
right click project in solution explorer -> Manage NuGet Packages -> search for MVC and install “Microsoft ASP.NET MVC”)
3.Close any and all open .cshtml files
4.Right click project -> Properties -> Build -> change Output path to your project “bin/”
5.Add the following minimal Web.config to the root of your class library project
( the web config file is solely needed for intellisense. Configuration (via Web.config)
should be done in the WebApplication hosting your ClassLibrary assembly)
6.Clean and Build the solution.
7.Open cshtml file and try now :)
I found that if it still doesn't work, you may need to go to the properties of the custom class and change the build action from "content" to "compile". That resolved it for me.
I try all of this solutions, one more thing which i didnt find is that in root web.config i must change webpages:Version from 2.0.0.0 to 3.0.0.0.
Open and close all .cshtml files and it's worked.
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
I'm trying to render Telerik Reporting (Q2 2014 SP1 - trial version) in my ASP.NET MVC 4 application (which uses .NET 4.5 framework), but for some reason "Telerik." namespace cannot be resolved in ASP.NET MVC Razor view, although I can access "Telerik." namespace in controller.
Razor view:
#using Telerik - error is in here
#Html.TelerikReporting() - error is in here
Web.config:
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="Telerik.Reporting" />
<add namespace="Telerik.ReportViewer.Mvc" />
</namespaces>
</pages>
</system.web.webPages.razor>
Telerik libraries my ASP.NET MVC project is referencing:
Telerik.Reporting
Telerik.ReportViewer.Mvc
What can be the issue with my set up?
I solved my problem.
I've been desperately trying following approaches: running solution in VS2012, deleted bin, obj folders, moving libraries from one path to another, restarting machine and VS numerous times and everything was futile.
However I've noticed that Telerik.Reporting.dll and Telerik.ReportViewer.Mvc.dll were not getting copied in my bin folder. In Visual Studio I expanded "References", clicked on each dll file and went to each dll's "Properties" and toggled "Copy Local" property from "False" to "True".
I cleaned and rebuilt my MVC project, checked that the Telerik dll files appeared in bin folder, went to my Razor view and #Html.TelerikReporting() worked!
Out of curiosity I toggled "Copy Local" property from "True" to "False", repeated the rest of the process and #Html.TelerikReporting() still worked. I don't know if there is a problem with Visual Studio or my "Professional" installation.
You shouldn't need a
#using Telerik
if the appropriate namespaces are imported in your web.config. Sometimes in order for your Razer views to pick up on the web.config namespaces after being initially added, you need to restart visual studio for some reason.
I would validate that "Telerik" is the appropriate namespace for the TelerikReporting method you're trying to access.
Additionally, you have a syntax error in:
#Html.TelerikReporting()
It should (likely) be
#(Html.TelerikReporting())
I'm having an issue where I keep getting error message CS0246 on my web server, but not in my visual studio environment. The issue is coming from a dll I reference in my project. The server cannot find my namespace for my dll.
To solve this I tried copying every file in my project directory to my web server, adding a tag in my web.config file, and adding an import statement in my aspx page. I have also tried adding an assembly reference in my aspx page, but that seems to require a change in the registry which I don't think I have access to.
I should also note my dll and namespace name are different, but changing the names of either did nothing. I also completely removed the dll and copied the code files directly into my project, but it still could not find the namespace.
I thus think I'm supposed to register the namespace and dll somewhere else, but I cannot figure out where. Any help will be appreciated.
If it matters, both my web project and dll are using .NET2.0
I know this is the equivalent of the annoying "did you reboot", but have you tried making a new solution and re-adding the projects and references? Sometimes the .sln file can get corrupted.
Sometimes, the web.config file contains instructions for the .NET framework to generate temporary files.
Please check your config file for something similar to:
<system.web.webPages.razor>
<host
factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="YourNamespace.ShouldBeCorrect.Here" />
</namespaces>
</pages>
</system.web.webPages.razor>
This is likely the case if your error page contains a reference to a dynamically generated file such as:
Source File: c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\133f2363\4ba6d0ba\App_Web_index.cshtml.a8d08dba.ociepbng.0.cs Line: 27