3

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.

2
  • Did you delete you bin and obj folders and rebuild? Commented Mar 28, 2018 at 11:34
  • @mjwills actually I just tried deleting the bin folder and rebuilding. But I've just tried deleting the obj folder as well, and it is still the same (that same error), thank you! Commented Mar 28, 2018 at 11:42

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

There is no way to change them on a project-wide basis I know about this, actually I changed the namespace fairly soon after creating the project, so the default namespace (that I had to find & replace) appeared in just a few places (usually just 1 class for a new library project). The problem here is even "find & replace" did not help me because the actual file it uses (in the Area created by code) was not included in the project, it's just used at runtime.
-1

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.

1 Comment

really I have a feeling that this might be a problem of Visual Studio 2017, but it's not very convenient for me to try it on VS 2015 because the code using C# 7.0, and I'm not sure if it can be opened with VS 2015 (for some compatibility issue), anyway thank you for the suggestion. If the problem is the cache, then it may seem to work fine on another computer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.