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 builder.UseContentRoot to tell MVC where the base folder is (the application base from which resolves the views\home\ folder)
call services.AddRazorPages().AddRazorRuntimeCompilation to set up Razor runtime compilation:
Host.CreateDefaultBuilder() .ConfigureWebHostDefaults (builder => { builder.UseContentRoot (@"your base folder"); builder.ConfigureServices (services => { services.AddRazorPages().AddRazorRuntimeCompilation (sa => Util.Framework.GetReferenceAssemblies (true) .Prepend (GetType().Assembly.Location) .ToList() .ForEach (sa.AdditionalReferencePaths.Add)); services.AddControllersWithViews(); }) .Configure (app => app.UseRouting().UseEndpoints (endpoint => endpoint.MapControllers())); }) .Build() .Run();
Here are the extra namespace imports that you need:
Microsoft.AspNetCore.Builder
Microsoft.AspNetCore.Hosting
Microsoft.AspNetCore.Mvc
Microsoft.Extensions.DependencyInjection
Microsoft.Extensions.HostingYou 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.
fail: Microsoft.AspNetCore.Server.Kestrel[13] Connection id "0HLUSTVQ934JD", Request id "0HLUSTVQ934JD:00000001": An unhandled exception was thrown by the application. System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.AspNetCore.Razor.Runtime, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified. File name: 'Microsoft.AspNetCore.Razor.Runtime, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext) at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(MetadataToken caCtorToken, MetadataImport& scope, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1& derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg) at System.Reflection.CustomAttribute.AddCustomAttributes(ListBuilder`1& attributes, RuntimeModule decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, ListBuilder`1 derivedAttributes) at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType) at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeAssembly assembly, RuntimeType caType) at System.Reflection.RuntimeAssembly.GetCustomAttributes(Type attributeType, Boolean inherit) at System.Attribute.GetCustomAttributes(Assembly element, Type attributeType, Boolean inherit) at System.Reflection.CustomAttributeExtensions.GetCustomAttributes[T](Assembly element) at Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemLoader.LoadAttributes(Assembly assembly) at Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemLoader.LoadItems(Assembly assembly) at Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RuntimeViewCompiler.CompileAndEmit(String relativePath) at Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RuntimeViewCompiler.OnCacheMiss(String normalizedPath) --- End of stack trace from previous location where exception was thrown --- at Microsoft.AspNetCore.Mvc.Razor.Compilation.DefaultRazorPageFactoryProvider.CreateFactory(String relativePath) at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.CreateCacheResult(HashSet`1 expirationTokens, String relativePath, Boolean isMainPage) at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.OnCacheMiss(ViewLocationExpanderContext expanderContext, ViewLocationCacheKey cacheKey) at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.LocatePageFromViewLocations(ActionContext actionContext, String pageName, Boolean isMainPage) at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.FindView(ActionContext context, String viewName, Boolean isMainPage) at Microsoft.AspNetCore.Mvc.ViewEngines.CompositeViewEngine.FindView(ActionContext context, String viewName, Boolean isMainPage) at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.FindView(ActionContext actionContext, ViewResult viewResult) at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.ExecuteAsync(ActionContext context, ViewResult result) at Microsoft.AspNetCore.Mvc.ViewResult.ExecuteResultAsync(ActionContext context) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|29_0[TFilter,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters() --- End of stack trace from previous location where exception was thrown --- at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync() --- End of stack trace from previous location where exception was thrown --- at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker) at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)
-
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
<Query Kind="Program"> <NuGetReference>Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation</NuGetReference> <Namespace>Microsoft.AspNetCore</Namespace> <Namespace>Microsoft.AspNetCore.Builder</Namespace> <Namespace>Microsoft.AspNetCore.Hosting</Namespace> <Namespace>Microsoft.AspNetCore.Http</Namespace> <Namespace>Microsoft.AspNetCore.Mvc</Namespace> <Namespace>Microsoft.Extensions.DependencyInjection</Namespace> <Namespace>Microsoft.Extensions.Hosting</Namespace> <Namespace>Microsoft.Extensions.Logging</Namespace> <IncludeAspNet>true</IncludeAspNet> </Query>
Still the error from Kestrel
fail: Microsoft.AspNetCore.Server.Kestrel[13] Connection id "0HLV5GRQS3EK2", Request id "0HLV5GRQS3EK2:00000001": An unhandled exception was thrown by the application. System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.AspNetCore.Razor.Runtime, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.
-
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#betaLet 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