dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
34.87k stars 9.85k forks source link

.Net 8 SDK - AddRazorRuntimeCompilation error - Index.cshtml not found #52248

Open NISPico opened 7 months ago

NISPico commented 7 months ago

Is there an existing issue for this?

Describe the bug

We have an .net 6 project that runs MVC, and when we install the .Net 8 SDK on our local machine, and run the project, we get the error: The view 'Index' was not found. The following locations were searched: /Views/Content/Index.cshtml /Views/Shared/Index.cshtml /Pages/Shared/Index.cshtml

We found that adding

builder.Services.AddControllersWithViews().AddRazorRuntimeCompilation(); to program.cs

fixes the issue, but this could potentially break multiple sites, if the server running the site gets the .net 8 SDK

Expected Behavior

No response

Steps To Reproduce

Have an older .net project either 6 or 7 that runs mvc. Install .net 8 SDK on local machine Now the compiler cant find the index.cshtml file.

Exceptions (if any)

The view 'Index' was not found. The following locations were searched: /Views/Content/Index.cshtml /Views/Shared/Index.cshtml /Pages/Shared/Index.cshtml

.NET Version

.net 6

Anything else?

No response

rglobisch-bpc commented 6 months ago

I'm experiencing the same issue.

Alejatabares commented 5 months ago

I'm experiencing the same issue.

SalehAhmadi commented 5 months ago

This is how i managed to solve my issue. In Program.cs, I changed this line: .UseContentRoot(Directory.GetCurrentDirectory()) to this: .UseContentRoot(Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location)) and the problem was solved. Hope this helps.

LucGosso commented 3 months ago

Same problem when installed SDK 8.0.203 on local machine. .net6 solution broke. net8 too.

builder.Services.AddControllersWithViews().AddRazorRuntimeCompilation(); fixes it locally,

But now i have same problem then pushing the solution to web app in azure with LTS 8 on webapp. Had to restore backup to .net6.

UseContentRoot fix do not work on web app.

Swellenator commented 3 months ago

I also have this issue. Adding

builder.Services.AddControllersWithViews().AddRazorRuntimeCompilation(); fixes it on my machine, but when deployed to an Azure App Service the error is still there.

gamerwalt commented 2 months ago

Same issue here.

MarcelChirtes commented 2 months ago

I downgraded my dotnet sdk to 8.0.100 not it took me two hours to remember what I did last time to make it work...

$ dotnet --list-sdks
8.0.100 [C:\Program Files\dotnet\sdk]
8.0.101 [C:\Program Files\dotnet\sdk]
8.0.102 [C:\Program Files\dotnet\sdk]
8.0.204 [C:\Program Files\dotnet\sdk]

Then where .sln is located add global.json with

{
  "sdk": {
    "version": "8.0.100"
  }
}

Until they provide a real solution here, for me .AddRazorRuntimeCompilation() did not worked, was not recognized even with the nuget packaged installed, maybe because it's a shared razor library.

Scriptman commented 1 month ago

I'm currently having the same issue.

Debugging with VS is working fine (runtime compilation enabled).

But publishing is not working.

Compiled on my laptop, running 8.0.200, results in InvalidOperationException: The view 'Index' was not found. Compiled on an Ubuntu server running 8.0.105, results in InvalidOperationException: The view 'Index' was not found.

So my site is looking fantastic online with a 500 - Internal Server Error page :)

simon-wacker commented 2 weeks ago

I experience the same error in the branch https://github.com/building-envelope-data/metabase/pull/187 of a public project with the as-of-this-writing latest runtime 8.0.3 available on https://hub.docker.com/_/microsoft-dotnet-aspnet regarding for example the view /Views/Authorization/Logout. Enabling runtime compilation solves the issue in development but not in the published production version. In production, I publish the project in a Docker container based on the image Dockerfile-production.

simon-wacker commented 1 week ago

In my case, I figured out that view compilation does not retain the directory structure. For example, the view /Views/Authorization/Logout.cshtml is compiled as Logout.cshtml. The corresponding log output is Initializing Razor view compiler with compiled view: '/Logout.cshtml'. That is why the view lookup failed with the log outputs Could not find a file for view at path '/Views/Authorization/Logout.cshtml'. and Could not find a file for view at path '/Views/Shared/Logout.cshtml'. To fix the lookup I added a view-location format without directory structure by calling services.AddControllersWithViews().AddRazorOptions(_ => { _.ViewLocationFormats.Add("/{0}.cshtml"); }. I would rather have liked to change the view compileation such that it retains/respects the directory structure. I couldn't figure out how to do that though. I hope this helps someone somehow.