Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Native AoT in .net 7? #51

Closed
kurtcodemander opened this issue Oct 24, 2022 · 2 comments
Closed

Support for Native AoT in .net 7? #51

kurtcodemander opened this issue Oct 24, 2022 · 2 comments

Comments

@kurtcodemander
Copy link

I'm looking for a Razor templating solution which could work with AoT compilation in .net 7, with <PublishAot>true</PublishAot>.

When visiting http://localhost:5000 the following example correctly returns text/html and the rendered view if starting with debugging (without AoT). But after publishing with AoT it returns application/json and emty json {} in the browser.

Why? Is it possible to make it work with AoT?

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
	  <RuntimeIdentifier>win-x64</RuntimeIdentifier>
	  <RazorCompileOnBuild>true</RazorCompileOnBuild>
	  <RazorCompileOnPublish>true</RazorCompileOnPublish>
	  <SelfContained>true</SelfContained>
	  <OutputType>Exe</OutputType>
	  <PublishReadyToRun>true</PublishReadyToRun>
	  <PublishTrimmed>true</PublishTrimmed>
	  <PublishAot>true</PublishAot>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Razor.Templating.Core" Version="1.8.0-rc.1" />
  </ItemGroup>

</Project>
using Razor.Templating.Core;

namespace AotTest1; 

public class ExampleModel
{
	public string PlainText { get; set; }
	public string HtmlContent { get; set; }
}

public class Program
{		
	public static void Main(string[] args)
	{
		var builder = WebApplication.CreateBuilder(args);

		builder.Services.AddAuthorization(); 

		var app = builder.Build();

		app.UseStaticFiles();

		app.UseRouting();

		app.UseAuthorization();

		app.MapGet("/", HandleRequest);

		app.Run();
	}

	private static async Task<IResult> HandleRequest(HttpRequest request, HttpResponse response)
	{			
		var model = new ExampleModel()
		{
			PlainText = "This text is rendered from Razor Views using Razor.Templating.Core",
			HtmlContent = "<em>You can use it to generate email content, report generation and so on</em>"
		};

		var html = await RazorTemplateEngine.RenderAsync("/Views/ExampleView.cshtml", model, viewDataOrViewBag);

		return Results.Text(html, "text/html"); 

	}
}

@model AotTest1.ExampleModel

@{
	Layout = null;
}

<!DOCTYPE html>
<html>
<head>
</head>
<body>
	<div>
		@Model.PlainText
	</div>
</body>
</html>
@soundaranbu
Copy link
Owner

Hi @kurtcodemander, thanks for raising the issue. Although I wish that I could add support for NativeAOT, the underlying ASP.NET Core MVC itself needs support for it. Only then, it will be possible for our library to add support as it's a wrapper around the MVC. As of .NET 7 timeframe, we could see that the NativeAOT is mainly focused on console applications. So let's hope that MVC will be supported in the near future and we'll make our library support it too :)

@kurtcodemander
Copy link
Author

Thanks @soundaranbu, I found a great micro-framework by David Fowler which is AOT friendly and suits my needs for now as a workaround. Although it doesn't support Razor.

https://github.com/davidfowl/uController

I'll keep an eye on your repository for the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants