Search code examples
c#asp.net-core-mvcasp.net-core-2.0dotnet-cli

Moving project to another directory causes missing view


I created a project with the dotnet cli and everything worked up until the time I wanted to write tests for the app.
I created a src directory and moved the entire web project to the src folder after which I created the test project in a folder called tests.

I added reference to the web app in the test project, the test run successfully but when I run my app with dotnet run I get the following error:

enter image description here

The Views folder is in the src directory and every other files including the *.csproj.

Please find my folder structure below:

Folder Structure

Any help would be appreciated. Thank you.


Solution

  • Update:

    I see what your problem is now, in launch.json ,change

    cwd": "${workspaceFolder}",
    

    to

    cwd": "${workspaceFolder}\\src",
    

    This is related to vs code,you need to tell it which folder is the workspace
    Your code was working fine on Visual Studio
    Also,don't forget to remove

    products.Add("Error", null);
    

    Original Post:

    Not sure which is your current folder structure but let's take your original example.

    If you just move the "Views" folder inside a src folder you can add the following in your Startup.cs file :

        public void ConfigureServices(IServiceCollection services)
        {
            //..
            services.Configure<RazorViewEngineOptions>(o =>
            {
                for (int i = 0; i < o.ViewLocationFormats.Count; i++)
                    o.ViewLocationFormats[i] = "/src" + o.ViewLocationFormats[i];
            });
        }
    

    Now,if you moved other resources too(css,images) you'll have to update any references to them.
    I don't see why you should deviate from the convention though,create a new clean project ,with tests and see what's different from you project