4

I recently migrated my ASP.NET Core MVC Project to ASP.NET Core 2.0. The web application was migrated using Visual Studio 2017 version 15.3 Preview.

The application works fine in the development mode ( both from Visual studio and using the console dotnet run ) but when i publish it and host it in IIS (Version 10 ), it throws and error saying :

The view 'Index' was not found. The following locations were searched: /Areas/Admin/Views/Shared/Home/Index.cshtml ...

Note : I have a custom route configuration where the views are configured using a Custom ViewLocationExpander

This is what my project structure looks like :

enter image description here

Things that i have tried are :

In the .csproj :

<PreserveCompilationContext>true</PreserveCompilationContext>

In the Program.cs :

.UseContentRoot(Directory.GetCurrentDirectory())

I have configured the RazorViewEngineOptions as follows :

enter image description here

Including the following in .csproj did not work as well :

<ItemGroup>
    <Content Update="wwwroot\**\*;**\*.cshtml;appsettings.json;web.config">
        <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
    </Content>
</ItemGroup>

If i try including the .cshtml files manually, it throws a duplicity error.

I have gone through a lots of articles and the official github links, but nothing is working.

Am i missing something out here ? Any suggestions would be greatly appreciated !!

Tseng
  • 61,549
  • 15
  • 193
  • 205
Ozesh
  • 6,536
  • 1
  • 25
  • 23
  • To avoid confusion please avoid using outdated (MVC 6) or confusing terms (DotNetCore/.NET Core, when you actually talk about ASP.NET Core running on .NET Core). ASP.NET Core is a webstack for creating Web applicatino. .NET Core is **a runtime**, one of **two** on which ASP.NET Core 1.x/2.x can run on. The other is full .NET Framework – Tseng Jun 20 '17 at 12:25
  • Sure. I thought a normal .Net developer would understand without any confustion. I even had included the tags in case that happens. Any ways, Thanks. – Ozesh Jun 22 '17 at 04:54
  • It was called MVC6 and ASP.NET 5 for some times, but it did rise expectations to be successor of ASP.NET 4.5 and MVC 5, which it isn't. It's a complete new rewrite, which is in no way compatible to the old ASP.NET / MVC5 webstack. That being said, there is still a chance that a real MVC6 comes (which is based on MVC5), so still calling ASP.NET Core "MVC6" will end up being double confusing – Tseng Jun 22 '17 at 05:54

1 Answers1

2

I solved it finally.

First of all, I had to include the following in the PropertyGroup in the .csproj file

<MvcRazorExcludeRefAssembliesFromPublish>false</MvcRazorExcludeRefAssembliesFromPublish>

It must be because of my distributed Views structure.

I dont know why, but i also had to separately publish my Views folders (Views and Areas ) after publishing my application.

Note: Including the Views folders in .csproj for publishing did not help

After publishing my views, it gave an error in the browser saying Cannot find compilation library location for package 'Microsoft.Win32.Registry'.

Hence, I had to install the Microsoft.Win32.Registry package and publish my folder once again.

And that is how it worked.

Hope, it help someone facing a similar kind of issue.

Ozesh
  • 6,536
  • 1
  • 25
  • 23
  • This was exactly what I needed. I had to separately publish my views folder as you stated and my wwwroot folder. Thank you for sharing! – Eric Dec 20 '18 at 19:36