Hopeless
Hopeless

Reputation: 4773

Compilation error after changing ASP.NET MVC project namespace?

I'm sure that this is a bug of Visual Studio (2017 if it matters).

I would like to keep the project name short and simple but the project namespace should be verbose enough to follow my naming convention. So after creating the project, I changed the project namespace in the Application tab in project properties window. Building is OK, but debugging showed that there was some compilation error (will be soon posted below). I've Googled around and found out that the problem is in the web.config inside the Views folder, that looks like the last place the old namespace referenced. I've also tried changing it to the new one and the first time it seemed to work OK.

But now I've encountered that exact same issue again and of course the web.config file I fixed before is not changed to something wrong, the new namespace is there (replacing the old one). I've tried deleting all the cached files in /Local/Temp/Temporary ASP.NET Files, clean (manually deleting all files in /bin) and rebuild the project. Nothing worked. Still that same error. I've even tried restarting my computer and repeating the steps above but still nothing changed.

Here is the involved section in web.config:

<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="CS.MvcWebsite" />
  </namespaces>

The new namespace here is CS.MvcWebsite, while the old one is WebSite (which is also the project name). When running, it shows the compilation error like this:

enter image description here

I wonder why the hell that could still be pulled out? looks like there is still some very hidden place where the cache is saved. I hope someone here has experienced with this and successfully worked-around it. I'm sure that almost encountering this should have been fine by modifying the web.config file (like what I did too) but as I said strangely that's not enough to force VS to use what is new, it looks really like a bug.

Update: After suggesting of mjwills, I've also tried deleting the obj folder as well (as before only bin was cleaned). But still it does not help anyway.

Upvotes: 3

Views: 1720

Answers (3)

NightOwl888
NightOwl888

Reputation: 56909

I changed the project namespace in the Application tab in project properties window.

No, you didn't. You changed the default namespace for the project, which is only used when a new .cs file is added to the project.

The namespaces that are actually defined in C# are declared inside of the .cs files of the project. There is no way to change them on a project-wide basis.

namespace Website // This is the namespace declaration
{
    public class Foo
    {
    }
}

That said, it is pretty simple to change all of the namespaces at once in the .cs files with Visual Studio's Find and Replace feature.

enter image description here

Also, as you have discovered there are other places that reference the namespaces inside of Web.config and Views/Web.config (and possibly other places) that may need to be changed to match changes to namespaces in your .cs files.

This is not a bug with Visual Studio. Visual Studio has no built-in feature to deal with changing project namespaces other than Find and Replace, which is adequate enough since namespaces are very rarely changed in practice. When doing such changes, it is not unusual to have to hand edit files to make sure you synchronize the changes across the entire project and the files that need to be edited vary depending on what type of project it is, what features it is using, etc.

Upvotes: 0

Hopeless
Hopeless

Reputation: 4773

Well actually the view (navigated to) causing that error is from a separate project in Areas. So there is another one Web.config file I need to update is under Areas folder. In fact this is auto-copied by a library (supporting to organize the MVC projects into modules). It's very hidden because it is not part of the solution (so searching inside the solution for the old namespace won't work).

I'm so sorry to Visual Studio 2017 because of blaming a possible bug on it.

Upvotes: 3

Nikolay Hristov
Nikolay Hristov

Reputation: 39

The whole story with namespaces and VS 2017 is a nightmare. Recently I created a new library and had some mixed up namespaces, so I fixed them and re-packed the nuget, but after consuming the nuget package the namespaces where the same. I lost 2 hours deleting cache, restarting the computer, etc. Finally just got out another laptop, moved the code with a flash drive and it worked properly. I would suggest you try building the project with another pc, if the problem persists you probably have some problems in the csproj or in the root sector of the web.config.

Upvotes: -1

Related Questions