Where does Razor engine search for View file
- Linqpad version 6.7.5
- Basic AspNetCore Mvc snippet code
void Main() { Directory.SetCurrentDirectory(Path.GetDirectoryName(Util.CurrentQueryPath)); Host.CreateDefaultBuilder() .ConfigureWebHostDefaults(builder => builder.ConfigureServices(services=>services.AddControllersWithViews()) .Configure(app=>app.UseRouting().UseEndpoints(endpoint=>endpoint.MapControllers()))) .Build() .Run(); } public class HomeController : Controller { [HttpGet("/")] public ViewResult Index() { return View(); } }
- The following error
info: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker[2] Executed action HomeController.Index (LINQPadQuery) in 110.6171ms info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1] Executed endpoint 'HomeController.Index (LINQPadQuery)' fail: Microsoft.AspNetCore.Server.Kestrel[13] Connection id "0HLUJEGPERA0S", Request id "0HLUJEGPERA0S:00000001": An unhandled exception was thrown by the application. System.InvalidOperationException: The view 'Index' was not found. The following locations were searched: /Views/Home/Index.cshtml /Views/Shared/Index.cshtml
My question is where I should put index.cshtml file?
Comments
You need to do two things to make this work:
call services.AddRazorPages().AddRazorRuntimeCompilation to set up Razor runtime compilation:
Here are the extra namespace imports that you need:
Microsoft.AspNetCore.Builder
Microsoft.AspNetCore.Hosting
Microsoft.AspNetCore.Mvc
Microsoft.Extensions.DependencyInjection
Microsoft.Extensions.Hosting
You will also need to add the following NuGet package:
Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
Note that right now, the call to Util.Framework.GetReferenceAssemblies (true) will not compile because this overload (that accepts a Boolean parameter to indicate that ASP.NET reference assemblies are required) has not yet been released in a beta. A beta with feature will be available within a few days.
Thanks for the quickly reply. I am looking forward for the new release.
I have tried the new version 6.8.1. Now the error moves to Microsoft.AspNetCore.Razor.Runtime.
In Query Properties (F4), have you ticked the checkbox Reference ASP.NET Core assemblies?
The only NuGet package you should need is Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.
Yes. Here is the header for the linq
Still the error from Kestrel
I've identified the problem and will aim to get a fix out in the next day or two.
The ALC issue with ASP.NET assemblies has been fixed in 6.8.1:
https://www.linqpad.net/LINQPad6.aspx#beta
Let me know how you get along.
Terrific. It works now. But the new beta version is
6.8.2
NOT6.8.1
. I think this is just your typo.Great. Let me know if there any more glitches. It would be good to make LINQPad robust in this scenario.
@JoeAlbahari I have no idea but my razor engine view file seems not working:
I'm using latest 6.8.2 version, just tell me what's wrong