Tuesday, 15 April 2014

asp.net mvc - How to prevent razor from escaping -



asp.net mvc - How to prevent razor from escaping -

i trying add together youtube video code dynamically link, razor escapes @ sign.

class="snippet-code-html lang-html prettyprint-override">//www.youtube.com/embed/@model.mallvideos.where( mv => mv.isfeatured).firstordefault().video.code

it work if write embed/ @model or embed /@model uri broken.

what mean of broken html when result rendered browser see:

class="snippet-code-html lang-html prettyprint-override">http://www.youtube.com/embed/@model.mallvideos.where( mv => mv.isfeatured).firstordefault().video.code

instead of

class="snippet-code-html lang-html prettyprint-override">http://www.youtube.com/embed/fghnkhyth6uh

interestingly when utilize /@model in other place in urls on same view works, particular url acts strangely. refrence, code not in other code block, , straight placed view.

how can forcefulness razor allow me task without need creating custom function, or extension method.

use parentheses explicit look is, described in c# razor syntax quick reference

//www.youtube.com/embed/@(model.mallvideos.where( mv => mv.isfeatured).firstordefault().video.code)

html extension method

public static mvchtmlstring fullurl(this htmlhelper htmlhelper, string baseaddress, string relativeuri, string innertext = "") { uri baseuri = new uri(baseaddress); uri fulluri = new uri(baseuri, relativeuri); var builder = new tagbuilder("a"); builder.mergeattribute("href", fulluri.tostring()); if (string.isnullorempty(innertext)) { builder.innerhtml = fulluri.tostring(); } else { builder.innerhtml = innertext; } homecoming mvchtmlstring.create(builder.tostring(tagrendermode.normal)); }

cshtml

@html.fullurl( "http://www.youtube.com/embed/", model.mallvideos.where( mv => mv.isfeatured).firstordefault().video.code)

resulting html

<a href="http://www.youtube.com/embed/12345">http://www.youtube.com/embed/12345</a>

i have tried reproduce issue , i'm not able reproduce when utilize parentheses.

models

namespace project.models { public class videosviewmodel { public list<mallvideo> mallvideos { get; set; } } public class mallvideo { public bool isfeatured { get; set; } public video video { get; set; } } public class video { public string code { get; set; } } }

controller

public class homecontroller : controller { random rnd = new random(); public actionresult index() { var model = new videosviewmodel(); model.mallvideos = getvideos(10); homecoming view(model); } public list<mallvideo> getvideos(int length) { var videos = new list<mallvideo>(); (int = 0; < length; i++) { videos.add(new mallvideo() { isfeatured = (rnd.nextdouble() > 0.5), video = new video() { code = path.getrandomfilename() } }); } homecoming videos; } }

view

note: http://www.youtube.com/embed/@(model.mallvideos.where(mv => mv.isfeatured).firstordefault().video.code)

@model project.models.videosviewmodel <fieldset> <legend>videosviewmodel</legend> <p> http://www.youtube.com/embed/@(model.mallvideos.where(mv => mv.isfeatured).firstordefault().video.code) </p> <p> @model.mallvideos.where(mv => mv.isfeatured).firstordefault().video.code </p> </fieldset>

resulting html

<html> <body> <fieldset> <legend>videosviewmodel</legend> <p> http://www.youtube.com/embed/013dq0j5.dr1 </p> <p> 013dq0j5.dr1 </p> </fieldset> </body> </html>

asp.net-mvc razor

No comments:

Post a Comment