Is it possible to deploy an uncompiled ASP.NET Razor Pages website?

蓝咒 提交于 2020-07-22 05:04:30

问题


I am wondering if it is possible to copy the source code of an ASP.NET Razor Pages website to a Windows web server and for it to run without it needing to be published/compiled?

Similar to what you can do with ASP.NET Web Forms websites where you upload the source code (*.aspx and *.aspx.cs files) and they compile at runtime?

I saw something about Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation but I wasn't sure if this is what I was after and how to use it.

Is this possible and any guidance or links on how to do it?

PS. I'm sure this is not good practice, but would still like an answer... Thanks.


回答1:


Is this possible and any guidance or links on how to do it?

PS. I'm sure this is not good practice, but would still like an answer

There's a way to allow deploy "uncompiled" *.cshtml. Assuming you're using ASP.NET Core 3.1:

  1. First of all, add a package reference to Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation:

    <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.1.*" />
    
  2. Change your services to allow Runtime Compilation:

     services.AddRazorPages().AddRazorRuntimeCompilation();
     
  3. Configure a custom Task to copy the source code to publish dir in your *.csproj file:

    <ItemGroup>
        <ViewFiles Include="$(ProjectDir)\Pages\**\*.cshtml" />
    </ItemGroup>
    <Target Name="CopyViewFilesAfterPublish" AfterTargets="Publish">
        <Copy SourceFiles="@(ViewFiles)" 
          DestinationFolder="$(PublishDir)\Pages\%(RecursiveDir)" 
        />
    </Target>
    

Demo

I publish a RazorPage WebApp and host it on IIS. And then we can change the Pages/**/*.cshtml views dynamically:



来源:https://stackoverflow.com/questions/59279396/is-it-possible-to-deploy-an-uncompiled-asp-net-razor-pages-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!