Razor and HTML mixed output traps and tricks

Source: Internet
Author: User

3,razor and HTML mixed output traps and tricks

Property name MiscalculationSometimes we have to make the HTML content with the Razor statement immediately, eg: Hello, Mr. A. Suppose the variable name is viewbag.name, hello, @ViewBag. Name//So the output is only "Hello," and the latter name is treated as a ViewBag attribute went, so if you want to solve this problem is Add a parenthesisGood, such as "Hello, (@ViewBag. Name)" and one more way Wrap it up with an HTML tag, such as "Hello,< tags > @ViewBag. name</tag > sir", or to wrap it up with HTML tags. Unexpected extra attributesIf you want to output a C # namespace, and the output part of the north wing to become a razor variable, the category is System.Web.Mvc.Controller eg:@{viewbag.mvcnamespace= "SYSTEM.WEB.MVC";} Class is @viewbag.mvcnamespace.controller the result output throws an error, and the string does not contain the controller definition because razor resolves @ When viewbag.mvcnamespace the controller as a controller property of the @viewbag.mvcnamespace, parsing the error, the solution to this situation is to add a parenthesis,Eg: @ (viewbag.mvcnamespace). Controller, it will output "SYSTEM.WEB.MVC" output email address with @ Hop-off sub-element< hyperlink a href= "[email protected]" >aaa Add a parenthesisWe can, output a string without HTMLEncodePresets use razor syntax to output variables, all content presets will be HTML encoded as HTMLEncode, this is to protect the Web page against cross-site scripting attacks, with this default, to ensure that the novice write unsafe code eg:@{viewbag.description= " < label style= ' font-size:12px; ' > Description Text </label > ";} @ViewBag. Description The result of the final output is a HTMLEncode version: the greater-than-sign double quotation marks have become the form of & plus some letters, and the solution is to use the @html.raw helper method to help us output the content @Html. Raw(viewbag.description) This will be the same as the output we want to look like.

Razor and HTML mixed output traps and tricks

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.