6

I have a razor snippet stored in a string:

@if (Request.IsAuthenticated)
{
    <h1>Hello World</h1>
}

I store the code snippet in my cache. How can I inject the string such that it renders as if it was part of the original view file, executing the code also?

I have tried:

@ApplicationCache.GetDynamicString("MyString")
@Html.Raw(ApplicationCache.GetDynamicString("MyString"))

These methods return a string, not an MvcHtmlString. I could dynamically write to a partial view from my server and render the partial view, but I'm wondering if there's a helper I'm missing.

David James Ball
  • 903
  • 10
  • 26
  • this is actually a very good question – Ian CT Feb 27 '15 at 19:03
  • Why not try to build a helper to do that? – Rafael A. M. S. Feb 27 '15 at 19:05
  • 2
    Weird. I would have thought `Html.Raw` would have worked. What about doing `@MvcHtmlString.Create(ApplicationCache.GetDynamicString("MyString"))`? – David Sherret Feb 27 '15 at 19:05
  • Agree with @DavidSherret. MvcHtmlString.Create() should work for you – Pat Burke Feb 27 '15 at 19:06
  • It seems like you've got a Catch-22 situation here. The `Raw` call injects the string into the View, but the Razor rendering engine never gets a chance to parse and execute it because it's already past that point. You'd almost need a multi-pass parser of the Razor view, the first pass allowing you to inject new code into the view and the second pass to then parse and execute that new code. – Craig W. Feb 27 '15 at 19:08
  • I agree this is a catch-22. I think I will have to go with writing to a partial view file in the background. To the others commenting on Html.Raw, that won't execute any c# code. It just outputs that as plain-text. – David James Ball Feb 27 '15 at 19:12
  • @DavidJamesBall: Use RazorEngine from my proposed duplicate. That *is* able to render the 'view in code'. – Patrick Hofman Feb 27 '15 at 19:13
  • Are you essentially trying to compile partial view from string? (Patrrick Hofman's suggestion is possible approach in this case). – Alexei Levenkov Feb 27 '15 at 19:13
  • Oh... I assumed the code to execute was JavaScript and not C#. – David Sherret Feb 27 '15 at 19:13
  • @PatrickHofman I guess it depends on whether it's important to maintain the current view context and/or model metadata. I'd be interested in seeing a solution that assumes it is. – Ant P Feb 27 '15 at 19:19
  • @AntP: I agree, but that is not clear from the context. I would be happy to reopen if OP tells it *is* necessary. – Patrick Hofman Feb 27 '15 at 19:20
  • 1
    I solved this by using razor engine as highlighted by Patrick Hofman. I pass the Request.Authenticated value to a helper which builds a model from the value, injecting it into my string template, and renders it back out. – David James Ball Feb 27 '15 at 19:31
  • @DavidJamesBall: Glad you fixed it. If you want an answer on the part Ant P put here, you might want to edit your question and I will reopen. If not, let's keep it this way. – Patrick Hofman Feb 27 '15 at 19:33

0 Answers0