0

I'm using the Razor View Engine to Generate HTML outside of a MVC app (In a win forms)

Most of my Razor Views work pretty logically, but I'm having issues displaying an image.

I'm trying to display a PNG image that's been retrieved from a database. Since I'm in a winforms app a lot of the helper functions I'd normally use are absent, so I'm trying to do it inline.

 string base64 = Convert.ToBase64String( @Context.Model.ETA640StudentProfileVM[ currentRecord ].ImageObj );
imageBytes);
<img src="@String.Format( "data:image/png;base64,{0}", base64 )" />

But when I evaluate this code to display the image using the code above, which I got here:

StackOverflow Article

I get this error out of the Razor Renderer Line: 305\t Col: 1\t Error: The name 'WriteAttribute' does not exist in the current context

called like this:

return RenderTemplate(sourceCodeReader, referencedAssemblies, context, null);

The Razor engine is what is throwing the error... All my other HTML works fine so far...

Here is the code the Razor View Engine is outputting for the statement (from the error text)

WriteLiteral("  <img");
WriteAttribute("src", Tuple.Create(" src=\"", 6855),
Tuple.Create("\"", 6915) , Tuple.Create(Tuple.Create("", 6861),
Tuple.Create<System.Object, System.Int32>(String.Format(
@"data:image/png;base64,{0}", base64 ) , 6861), false) );

WriteLiteral(" />\r\n");

As you can see, the WriteAttribute is inside the Quotes for the WriteLiteral statement... This seems pretty evidently incorrect.

Anyone got any idea why that pair of lines causes the Razor engine to Puke?

Has anyone used the Razor engine to generate HTML to display a PNG frim the Database and had it work outside of an MVC app?

(P.S. I had another question that approached this same error from a much worse perspective, that question has been deleted)

Edit: For those who follow, the issue appears to be directly related to parsing a variable in the path of an image tag.. for example this works perfectly.

<img src="D:\Project Files\EFolderReportGenerator3G\WinForm\EFolderReportGenerator3G\bin\Debug\Tmp.png" border="0" height="155" />

But this dies horribly (both double slashing and using the @"" string literal way of dealing with the '\' chaaracters)

   @string path = "D:\\Project Files\\EFolderReportGenerator3G\\WinForm\\EFolderReportGenerator3G\\bin\\Debug\\Tmp.png";
   <img src="@path" border="0" height="155" />

Now I'm trying to solve the relative path issue so my solution is portable.

Community
  • 1
  • 1
Eric Brown - Cal
  • 14,135
  • 12
  • 58
  • 97

1 Answers1

1

The code:

<img src="@String.Format( "data:image/png;base64,{0}", base64 )" />

Isn't being parsed correctly by StackOverflow because of the quotes for the src attribute and the Format method argument aren't obvious, I'm not sure if the Razor engine can follow either. Maybe you could try moving the string format to an assignment and reference that variable instead of calling the method inline like that. It might at least reveal more about the problem.

Charlie
  • 8,530
  • 2
  • 55
  • 53
  • I tried this: var imgSrc = "data:image/png;base64," + base64; And got the exact same error... did you have something else in mind? Thanks. – Eric Brown - Cal Mar 04 '14 at 17:58
  • On your assumption (correct I think) that it's the parsing of that line that's causing the issue, I tried several ways to do it, and eventually wrote the image to the hard drive before rendering the razor view, and then referenced it by file name and it works... not a pretty solution, but good enough for now. Thanks! – Eric Brown - Cal Mar 04 '14 at 18:34
  • 1
    If you're generating temp files for those images, be cognizant that they might need to be cleaned up later. – Charlie Mar 04 '14 at 19:04
  • Already doing it, only write to one image file plus I was outputting stuff to a output folder to make testing easier, and it's wiped out at the begining of each execution :) Thanks, for having my back though :) – Eric Brown - Cal Mar 04 '14 at 19:59
  • 1
    Oddly this seems to work... I'm guessing the quotes and @ ar the issue...string path = "D:\\Project Files\\EFolderReportGenerator3G\\WinForm\\EFolderReportGenerator3G\\bin\\Debug\\Tmp.png"; @: – Eric Brown - Cal Mar 04 '14 at 20:33
  • Turns out Linq and HMTL helpers in MVC are not supported out of the box in the Razor ending using Rick Strahls hosting stuff.. just an FYI – Eric Brown - Cal Apr 16 '14 at 16:23