@Helper razor enconding html The Next CEO of Stack OverflowASP.NET MVC 4 HtmlHelper - mix decoded HTML with encoded HTMLHow do I import a namespace in Razor View Page?Escape @ character in razor view engineHow to use ternary operator in razor (specifically on HTML attributes)?With the new Razor View Engine, should my HtmlHelpers return string or IHtmlString?Using Razor within JavaScriptUnexpected Behavior Using MVC Razor's Html.Encode HTML HelperRetrieve Data from MVC 3 Razor Html Helper@Html Image helper displays image as html codeASP.NET MVC 4 HtmlHelper - mix decoded HTML with encoded HTMLAsp.net MVC Razor not encoding html

What happened in Rome, when the western empire "fell"?

How do I align (1) and (2)?

A Man With a Stainless Steel Endoskeleton (like The Terminator) Fighting Cloaked Aliens Only He Can See

Won the lottery - how do I keep the money?

How to count occurrences of text in a file?

Powershell. How to parse gci Name?

Is it ever safe to open a suspicious HTML file (e.g. email attachment)?

Is French Guiana a (hard) EU border?

INSERT to a table from a database to other (same SQL Server) using Dynamic SQL

Why didn't Khan get resurrected in the Genesis Explosion?

Is it convenient to ask the journal's editor for two additional days to complete a review?

Find non-case sensitive string in a mixed list of elements?

Writing differences on a blackboard

0 rank tensor vs 1D vector

What does "Its cash flow is deeply negative" mean?

Method for adding error messages to a dictionary given a key

Does soap repel water?

What is the value of α and β in a triangle?

RigExpert AA-35 - Interpreting The Information

Can we say or write : "No, it'sn't"?

Why the difference in type-inference over the as-pattern in two similar function definitions?

How to place nodes around a circle from some initial angle?

Where do students learn to solve polynomial equations these days?

The exact meaning of 'Mom made me a sandwich'



@Helper razor enconding html



The Next CEO of Stack OverflowASP.NET MVC 4 HtmlHelper - mix decoded HTML with encoded HTMLHow do I import a namespace in Razor View Page?Escape @ character in razor view engineHow to use ternary operator in razor (specifically on HTML attributes)?With the new Razor View Engine, should my HtmlHelpers return string or IHtmlString?Using Razor within JavaScriptUnexpected Behavior Using MVC Razor's Html.Encode HTML HelperRetrieve Data from MVC 3 Razor Html Helper@Html Image helper displays image as html codeASP.NET MVC 4 HtmlHelper - mix decoded HTML with encoded HTMLAsp.net MVC Razor not encoding html










0















According to https://blog.cindypotvin.com/character-encodings-with-razor-helpers/ and some other resources @Helper blocks return a HelperResult class that Implements IHtmlString.



Being and HtmlString all the string inside the @helper blocks should not be encoded.
But I have the following scenario (the real scenario is more complex) where the html is encoded



@helper Test()
@(new HtmlString("<label>Test 1</label>"))
@(new HtmlString("<label>Test 2</label>"))



When I check the view in browser I see literally <label> Test 1 .... because the Html code generate inside the @helper Test is between quotes.



But adding a RawString then it works correctly.



@helper Test()
@(new RawString(new HtmlString("<label>Test 1</label>").ToString()))
@(new RawString(new HtmlString("<label>Test 2</label>").ToString()))



Taking into account that HtmlString it´s for Razor to not encode a string and @Helper returns HtmlString, I don´t get why I need to add RawString to render correctly the html code on browser.










share|improve this question
























  • stackoverflow.com/questions/15765539/…

    – mxmissile
    Mar 8 at 15:28











  • @PeterB Done. Sorry I was in a computer without Visual Studio

    – Marc Cals
    Mar 8 at 15:42















0















According to https://blog.cindypotvin.com/character-encodings-with-razor-helpers/ and some other resources @Helper blocks return a HelperResult class that Implements IHtmlString.



Being and HtmlString all the string inside the @helper blocks should not be encoded.
But I have the following scenario (the real scenario is more complex) where the html is encoded



@helper Test()
@(new HtmlString("<label>Test 1</label>"))
@(new HtmlString("<label>Test 2</label>"))



When I check the view in browser I see literally <label> Test 1 .... because the Html code generate inside the @helper Test is between quotes.



But adding a RawString then it works correctly.



@helper Test()
@(new RawString(new HtmlString("<label>Test 1</label>").ToString()))
@(new RawString(new HtmlString("<label>Test 2</label>").ToString()))



Taking into account that HtmlString it´s for Razor to not encode a string and @Helper returns HtmlString, I don´t get why I need to add RawString to render correctly the html code on browser.










share|improve this question
























  • stackoverflow.com/questions/15765539/…

    – mxmissile
    Mar 8 at 15:28











  • @PeterB Done. Sorry I was in a computer without Visual Studio

    – Marc Cals
    Mar 8 at 15:42













0












0








0


1






According to https://blog.cindypotvin.com/character-encodings-with-razor-helpers/ and some other resources @Helper blocks return a HelperResult class that Implements IHtmlString.



Being and HtmlString all the string inside the @helper blocks should not be encoded.
But I have the following scenario (the real scenario is more complex) where the html is encoded



@helper Test()
@(new HtmlString("<label>Test 1</label>"))
@(new HtmlString("<label>Test 2</label>"))



When I check the view in browser I see literally <label> Test 1 .... because the Html code generate inside the @helper Test is between quotes.



But adding a RawString then it works correctly.



@helper Test()
@(new RawString(new HtmlString("<label>Test 1</label>").ToString()))
@(new RawString(new HtmlString("<label>Test 2</label>").ToString()))



Taking into account that HtmlString it´s for Razor to not encode a string and @Helper returns HtmlString, I don´t get why I need to add RawString to render correctly the html code on browser.










share|improve this question
















According to https://blog.cindypotvin.com/character-encodings-with-razor-helpers/ and some other resources @Helper blocks return a HelperResult class that Implements IHtmlString.



Being and HtmlString all the string inside the @helper blocks should not be encoded.
But I have the following scenario (the real scenario is more complex) where the html is encoded



@helper Test()
@(new HtmlString("<label>Test 1</label>"))
@(new HtmlString("<label>Test 2</label>"))



When I check the view in browser I see literally <label> Test 1 .... because the Html code generate inside the @helper Test is between quotes.



But adding a RawString then it works correctly.



@helper Test()
@(new RawString(new HtmlString("<label>Test 1</label>").ToString()))
@(new RawString(new HtmlString("<label>Test 2</label>").ToString()))



Taking into account that HtmlString it´s for Razor to not encode a string and @Helper returns HtmlString, I don´t get why I need to add RawString to render correctly the html code on browser.







asp.net-mvc razor






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 15:41







Marc Cals

















asked Mar 8 at 14:46









Marc CalsMarc Cals

2,10122138




2,10122138












  • stackoverflow.com/questions/15765539/…

    – mxmissile
    Mar 8 at 15:28











  • @PeterB Done. Sorry I was in a computer without Visual Studio

    – Marc Cals
    Mar 8 at 15:42

















  • stackoverflow.com/questions/15765539/…

    – mxmissile
    Mar 8 at 15:28











  • @PeterB Done. Sorry I was in a computer without Visual Studio

    – Marc Cals
    Mar 8 at 15:42
















stackoverflow.com/questions/15765539/…

– mxmissile
Mar 8 at 15:28





stackoverflow.com/questions/15765539/…

– mxmissile
Mar 8 at 15:28













@PeterB Done. Sorry I was in a computer without Visual Studio

– Marc Cals
Mar 8 at 15:42





@PeterB Done. Sorry I was in a computer without Visual Studio

– Marc Cals
Mar 8 at 15:42












1 Answer
1






active

oldest

votes


















2














This looks like a matter of incorrect expectations and/or interpretations...



A @helper method renders its output to an output stream, resulting in a plain text string. That string may contain HTML, and if so, then that HTML may or may not have been HTML encoded along the way (depending on how the helper was written). The resulting plain text string is then returned inside an IHtmlString wrapper object.



If an IHtmlString object is rendered using @(...), to Razor this means 'don't HTML-encode this, that was done already if/when/where needed'.



In short, doing @Test() sends the exact string content without further encoding to the browser (which then interprets it). If your aim was to encode it, then you could use @(Test().ToString()) because that pulls the string out of its IHtmlString protective cover, and then it will be HTML-Encoded.



If you inspect Test().ToString() then you can see the plain-text content, which for your Test() will look like "<label>...</label><label>...</label>". There's not a trace of the original HtmlString objects that you used, they were all rendered, and nothing was HTML-Encoded because you used @(new HtmlString(...)) for each.



It may be worth noting that these three @helper methods all have exactly the same output (except perhaps for some whitespace):



@helper Test()

<label>Test</label>


@helper Test()

@(new HtmlString("<label>Test</label>"))


@helper Test()

@(Html.Raw("<label>Test</label>"))



The result for all of these is the plain string "<label>Test</label>" wrapped as an IHtmlString.



Finally, if you would WANT the <label> to be HTML-Encoded, then this is what you'd need to use:



@helper Test()

@("<label>Test</label>")



The result of this is the plain string "&lt;label&gt;Test&lt;/label&gt;" wrapped as an IHtmlString.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55065579%2fhelper-razor-enconding-html%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    This looks like a matter of incorrect expectations and/or interpretations...



    A @helper method renders its output to an output stream, resulting in a plain text string. That string may contain HTML, and if so, then that HTML may or may not have been HTML encoded along the way (depending on how the helper was written). The resulting plain text string is then returned inside an IHtmlString wrapper object.



    If an IHtmlString object is rendered using @(...), to Razor this means 'don't HTML-encode this, that was done already if/when/where needed'.



    In short, doing @Test() sends the exact string content without further encoding to the browser (which then interprets it). If your aim was to encode it, then you could use @(Test().ToString()) because that pulls the string out of its IHtmlString protective cover, and then it will be HTML-Encoded.



    If you inspect Test().ToString() then you can see the plain-text content, which for your Test() will look like "<label>...</label><label>...</label>". There's not a trace of the original HtmlString objects that you used, they were all rendered, and nothing was HTML-Encoded because you used @(new HtmlString(...)) for each.



    It may be worth noting that these three @helper methods all have exactly the same output (except perhaps for some whitespace):



    @helper Test()

    <label>Test</label>


    @helper Test()

    @(new HtmlString("<label>Test</label>"))


    @helper Test()

    @(Html.Raw("<label>Test</label>"))



    The result for all of these is the plain string "<label>Test</label>" wrapped as an IHtmlString.



    Finally, if you would WANT the <label> to be HTML-Encoded, then this is what you'd need to use:



    @helper Test()

    @("<label>Test</label>")



    The result of this is the plain string "&lt;label&gt;Test&lt;/label&gt;" wrapped as an IHtmlString.






    share|improve this answer



























      2














      This looks like a matter of incorrect expectations and/or interpretations...



      A @helper method renders its output to an output stream, resulting in a plain text string. That string may contain HTML, and if so, then that HTML may or may not have been HTML encoded along the way (depending on how the helper was written). The resulting plain text string is then returned inside an IHtmlString wrapper object.



      If an IHtmlString object is rendered using @(...), to Razor this means 'don't HTML-encode this, that was done already if/when/where needed'.



      In short, doing @Test() sends the exact string content without further encoding to the browser (which then interprets it). If your aim was to encode it, then you could use @(Test().ToString()) because that pulls the string out of its IHtmlString protective cover, and then it will be HTML-Encoded.



      If you inspect Test().ToString() then you can see the plain-text content, which for your Test() will look like "<label>...</label><label>...</label>". There's not a trace of the original HtmlString objects that you used, they were all rendered, and nothing was HTML-Encoded because you used @(new HtmlString(...)) for each.



      It may be worth noting that these three @helper methods all have exactly the same output (except perhaps for some whitespace):



      @helper Test()

      <label>Test</label>


      @helper Test()

      @(new HtmlString("<label>Test</label>"))


      @helper Test()

      @(Html.Raw("<label>Test</label>"))



      The result for all of these is the plain string "<label>Test</label>" wrapped as an IHtmlString.



      Finally, if you would WANT the <label> to be HTML-Encoded, then this is what you'd need to use:



      @helper Test()

      @("<label>Test</label>")



      The result of this is the plain string "&lt;label&gt;Test&lt;/label&gt;" wrapped as an IHtmlString.






      share|improve this answer

























        2












        2








        2







        This looks like a matter of incorrect expectations and/or interpretations...



        A @helper method renders its output to an output stream, resulting in a plain text string. That string may contain HTML, and if so, then that HTML may or may not have been HTML encoded along the way (depending on how the helper was written). The resulting plain text string is then returned inside an IHtmlString wrapper object.



        If an IHtmlString object is rendered using @(...), to Razor this means 'don't HTML-encode this, that was done already if/when/where needed'.



        In short, doing @Test() sends the exact string content without further encoding to the browser (which then interprets it). If your aim was to encode it, then you could use @(Test().ToString()) because that pulls the string out of its IHtmlString protective cover, and then it will be HTML-Encoded.



        If you inspect Test().ToString() then you can see the plain-text content, which for your Test() will look like "<label>...</label><label>...</label>". There's not a trace of the original HtmlString objects that you used, they were all rendered, and nothing was HTML-Encoded because you used @(new HtmlString(...)) for each.



        It may be worth noting that these three @helper methods all have exactly the same output (except perhaps for some whitespace):



        @helper Test()

        <label>Test</label>


        @helper Test()

        @(new HtmlString("<label>Test</label>"))


        @helper Test()

        @(Html.Raw("<label>Test</label>"))



        The result for all of these is the plain string "<label>Test</label>" wrapped as an IHtmlString.



        Finally, if you would WANT the <label> to be HTML-Encoded, then this is what you'd need to use:



        @helper Test()

        @("<label>Test</label>")



        The result of this is the plain string "&lt;label&gt;Test&lt;/label&gt;" wrapped as an IHtmlString.






        share|improve this answer













        This looks like a matter of incorrect expectations and/or interpretations...



        A @helper method renders its output to an output stream, resulting in a plain text string. That string may contain HTML, and if so, then that HTML may or may not have been HTML encoded along the way (depending on how the helper was written). The resulting plain text string is then returned inside an IHtmlString wrapper object.



        If an IHtmlString object is rendered using @(...), to Razor this means 'don't HTML-encode this, that was done already if/when/where needed'.



        In short, doing @Test() sends the exact string content without further encoding to the browser (which then interprets it). If your aim was to encode it, then you could use @(Test().ToString()) because that pulls the string out of its IHtmlString protective cover, and then it will be HTML-Encoded.



        If you inspect Test().ToString() then you can see the plain-text content, which for your Test() will look like "<label>...</label><label>...</label>". There's not a trace of the original HtmlString objects that you used, they were all rendered, and nothing was HTML-Encoded because you used @(new HtmlString(...)) for each.



        It may be worth noting that these three @helper methods all have exactly the same output (except perhaps for some whitespace):



        @helper Test()

        <label>Test</label>


        @helper Test()

        @(new HtmlString("<label>Test</label>"))


        @helper Test()

        @(Html.Raw("<label>Test</label>"))



        The result for all of these is the plain string "<label>Test</label>" wrapped as an IHtmlString.



        Finally, if you would WANT the <label> to be HTML-Encoded, then this is what you'd need to use:



        @helper Test()

        @("<label>Test</label>")



        The result of this is the plain string "&lt;label&gt;Test&lt;/label&gt;" wrapped as an IHtmlString.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 8 at 17:59









        Peter BPeter B

        13.6k52046




        13.6k52046





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55065579%2fhelper-razor-enconding-html%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to get text form Clipboard with JavaScript in Firefox 56?How to validate an email address in JavaScript?How do JavaScript closures work?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I copy to the clipboard in JavaScript?How do I include a JavaScript file in another JavaScript file?Get the current URL with JavaScript?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

            Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

            List of MPs elected to the English parliament in 1640 (April) Contents List of constituencies and members See also Notes References Navigation menueNational Archives – The Glynde Place ArchivesCobbett's Parliamentary history of England, from the Norman Conquest in 1066 to the year 1803'Aldermen in Parliament', The Aldermen of the City of London: Temp. Henry III – 1912onepage&q&f&#61, false 229