@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

            Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

            Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

            How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page