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

Render Razor View or Partial to String #37187

Closed
wessleym opened this issue Oct 1, 2021 · 2 comments
Closed

Render Razor View or Partial to String #37187

wessleym opened this issue Oct 1, 2021 · 2 comments
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates design-proposal This issue represents a design proposal for a different issue, linked in the description

Comments

@wessleym
Copy link

wessleym commented Oct 1, 2021

Summary

I recently wanted to render a Razor page or partial to a string programmatically, and I've written a pretty good way to get it done by combining several solutions I've seen around the Web and testing them for performance. I'd like to contribute my solution to ASP.NET Core. It's an extension to HttpContext since it requires HttpContext's RequestServices.

Motivation and goals

This allows me to render a Razor partial to a string. It's especially useful for outputting HTML to be emailed.

In scope

Email a Razor view

Out of scope

Unknown

Risks / unknowns

  1. Is this an appropriate addition to ASP.NET Core? It's OK if it isn't. I just wanted to offer to contribute.
  2. I can never get this code to work for a .cshtml with @page at the top. It only seems to work with partials (no @page). Specifically, Model is null on the .cshtml page if it contains @page at the top. Again, this does not occur when no @page is at the top. I would like someone to provide a suggestion about that please.

Examples

It is invoked like this:
string html = await HttpContext.RenderViewAsync("Pages/SomePage.cshtml", new SomePageModel("X"), isMainPage: false);

I've attached the source code file. I couldn't attach a plain .cs file because GitHub apparently does not allow them to be uploaded in an issue.
ViewRendererHttpContextExtensions.zip

@wessleym wessleym added the design-proposal This issue represents a design proposal for a different issue, linked in the description label Oct 1, 2021
@Pilchie Pilchie added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Oct 1, 2021
@wessleym wessleym changed the title Render View or Partial to String Render Razor View or Partial to String Oct 4, 2021
@mkArtakMSFT
Copy link
Member

Thanks for contacting us.
It's great to see community building our their solutions for solving such problems. We think this would be a better fit to stay as a community project, rather be part of the framework.

@wessleym
Copy link
Author

wessleym commented Oct 6, 2021

@mkArtakMSFT, OK. Thanks.
If anyone ever comes looking for this feature, you can try this out:
https://github.com/wessleym/WessleyMitchell.Web.DotNetCore.ViewRenderer

@ghost ghost locked as resolved and limited conversation to collaborators Nov 6, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates design-proposal This issue represents a design proposal for a different issue, linked in the description
Projects
None yet
Development

No branches or pull requests

3 participants