I have no intellisense in my cshtml. I have in my web.config (views folder):
<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.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.Optimization"/>
<add namespace="EO2.Models" />
<add namespace="EO2" />
<add namespace="EO2.Resources"/>
</namespaces>
</pages>
</system.web.webPages.razor>
only when adding the #using MyNamespace in my cshtml it works.
I checked all versions numbers as desribed here:
MVC Razor view Intellisense broken in VS 2013/2015/2017
Intellisense in razor files (.cshtml) not work
Edit: Problem is in VS or other project files?
I have the same problem also in every new solution.
Tools->Import/Export Settings->Reset Settings
did it for me
tldr; Try switching build configurations and also deleting obj
I haven't had this problem for YEARS and suddenly started getting it.
>------ Build started: Project: MyMVC, Configuration: Debug Any CPU ------
6>S:\TFS\RRStore\RRStore.Main\MyMVC\obj\CodeGen\obj\Release\Package\PackageTmp\Areas\Store\Views\Products\Category\SearchPanel\Search.cshtml.cs(22,26,22,27): error CS0116: A namespace cannot directly contain members such as fields or methods
========== Build: 5 succeeded, 1 failed, 9 up-to-date, 0 skipped ==========
Wait a minute! I'm doing a DEBUG build and it is complaining about obj\Release
So I deleted obj completely and got this error:
15>------ Rebuild All started: Project: MicroPedi_MVC, Configuration: Debug Any CPU ------
15>...RazorGenerator.MsBuild.targets(44,9): error : Could not find a part of the path 'S:\TFS\RRStore\RRStore.Main\My_MVC\obj\Release\Package\PackageTmp\Areas\Admin\Views\CallCenter\Index.cshtml'.
========== Rebuild All: 14 succeeded, 1 failed, 0 skipped ==========
What!! That folder doesn't even exist (obj\Release)!
So I switched to Release configuration. Everything built. Now if I recall this doesn't prove anything because I think I'm using pre-compiled views - tbh I forget how it works.
Anyway THEN I switched back to Debug configuration.
Everything suddenly worked!
If that doesn't help - just try some basic detective work and assume nothing is working!
I found it was building fine on my server and I had made no changes to my web.config in a long long time.
Try making an inconsequential change to your file (to make sure the codegen is updating). eg. add a comment, classname or change some text
Find the generated file which will be something like \obj\CodeGen\obj\Release\Package\PackageTmp\Areas\Store\Views\Products\Products.cshtml.cs
See if your trivial change is reflected there
If not delete everything in \obj\CodeGen\... or probably in obj
Build your project
Another possibility:
I had recently installed .NET Framework 1.1 (to be able to extract a 3rd party library I needed).
I uninstalled it anyway, but not clear if this was the reason anything got screwed up. Suspicious though - especially since 1.1. always liked to mess with IIS.
I would suggest to:
Create a new MVC5 vs2019 project.
Navigate to web.config that resides under the views folder
Replace the 'broken' project web.config with the one from point 2
Fix missing project references
This should fix the error and make build possible again.
Related
I have an ASP.NET 4.7.2 Framework project. At runtime, upon loading a view, it says System.Runtime version 4.0.0.0 cannot be found. We have written about this extensively here: ASP.NET: .NET Framework/Standard/Core DLL collisions, caused by Nuget. "You must add a reference to assembly System.Runtime..."
It's been over a week and we still don't have a solution. All development has been halted.
Yesterday we tried side-by-side loading by simply calling on application start up:
Assembly systemRuntime = Assembly.Load("System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
Upon start up. We don't do anything with systemRuntime. It's only there for us to confirm that indeed 4.0.0.0 was loaded. Unfortunately, that didn't fix anything.
This makes me think that maybe since views are compiled at run-time, maybe somehow they are not referencing the right System.Runtime, or that we need to do the side-by-side loading there.
This problem is so severe we've started porting the site to .NET Core, because no one has any ideas how to solve it. According to analyzer, we'll have to rewrite approximately 15% of the site. We cannot go without deploying to production for potentially weeks, so a temporary solution must be found as soon as physically possible. We already have fixes that need to go to up.
Dai, in the first comment, directed me in the right direction: the view engine doesn't necessarily use the same references as the main project.
After some Googling, I found a way to reference assemblies for the view engine. In web.config:
<configuration>
...
<system.web>
<compilation debug="true" targetFramework="4.7.2" batch="false">
<assemblies>
<add assembly="System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</assemblies>
</compilation>
</system.web>
...
</configuration>
I also followed Dai's recommendation to try precompiling views. This is done by editing the MVC project's .csproj file with:
<MvcBuildViews>true</MvcBuildViews>
Precompiling the views is not necessary, but did prove it was indeed views compiling differently than the main project, and made it a tiny bit faster to test because I didn't have to run the site to see it blow up.
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 am deploying a new website as my main site and it works beautifully.
All of my applications under the root url work as well, except for one. It is a legacy system (c#.net) that is heavily used and unfortunately, I do not have access to the source code.
When I run the legacy application www.mysite.com/crm I get the following error:
Server Error in '/crm' Application.
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: CS0234: The type or namespace name 'Optimization' does not
exist in the namespace 'System.Web' (are you missing an assembly reference?)
Source Error:
Line 17: <pages>
Line 18: <namespaces>
Line 19: <add namespace="System.Web.Optimization" />
Line 20: </namespaces>
Line 21: <controls>
Source File: e:\WebSites\newsite\Web.config Line: 19
Show Detailed Compiler Output:
Show Complete Compilation Source:
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18446
The error is listing the web.config for the main site, which works. However, it happens only when I run the application under the main site.
This is running on Server 2008 R2 and IIS7
How do I correct this? What else can I try?
UPDATE
I don't know if it makes a difference, but the Web.Config it references in the error is for the new website, not for the legacy application. I am not sure why the main site config is a factor at all.
CLARIFICATION
I don't have access to the source code of the legacy application so I cannot adjust the properties for that application. Pasting the Optimization dll into the site didn't fix it either. It may work for other users, but not for me.
WORK AROUND
I setup the new site under another URL and redirect traffic to the root URL to the new URL and allow the legacy application to run as is until we can replace it. Very sloppy solution, but it works for now.
I just faced this issue now:
I reinstalled the optimization framework and everything worked fine:
Install-Package Microsoft.AspNet.Web.Optimization
I faced same problem after create a new area in MVC5. There is auto generate web.config file. Check your web.config file under Views folder there is a line
<add namespace="System.Web.Optimization" />
if you need this then install it using following command.
Install-Package Microsoft.AspNet.Web.Optimization
at Package Manager Console like below
If you think no need then simply remove <add namespace="System.Web.Optimization" /> line from following section in web.config file
<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="System.Web.Optimization" />-->
<add namespace="AutoParts.Web" />
</namespaces>
Check the Bin folder for the legacy application. It may simply need you to drop in that DLL. The System.Web.Optimization namespace exists as of ASP.NET 4.5 so it may not even be supported. The legacy App might be older than that.
In Visual Studio .. Go to your project and see the References tree.
right click System.Web.Optimization and choose Properties
look for the path , you will find it empty
if that is the case then
look in your PC for the file names System.Web.Optimization.dll and copy it in the bin folder of your project and that will resolve it
I was just experiencing: "CS0234 C# The type or namespace name 'Optimization' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)" and found this stackoverflow page. -- The cause for me was due to me renaming the web form (aspx page) I was working with, I also wanted to rename the public partial class behind that form. Doing so resulted in the mentioned error code. I added "using System.Web.Optimization;" to the code behind (C#), "cleaned" the solution, then "built" then solution, then "rebuilt" the solution and now the error is gone and that using statement wasn't being used, so I got rid of it. My solution no longer has that or any errors so I figured I'd share this weird story. I probably didn't need to use that "using" statement, but whatever.
Main to Area
#Html.ActionLink("My Area Home", "Index", "Home", new { area = "MyArea" }, null)
Area to Main
#Html.ActionLink("Home", "Index", "Home", new { area = "" }, null)
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