Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top