Why is Html.Raw escaping ampersand in anchor tag in ASP.NET MVC 4?

前端 未结 5 1709
旧时难觅i
旧时难觅i 2020-12-01 20:43

I have a URL I would like to render in an anchor tag as-is in a Razor view. I would have thought Html.Raw would be the way to go:

@{
    string test = \"http         


        
5条回答
  •  北荒
    北荒 (楼主)
    2020-12-01 21:30

    Like this:

    @{
        string test = "http://someurl.com/someimage.png?a=1234&b=5678";
    }
    
    
    

    produces valid markup:

    
    

    But this doesn't work. The ampersand gets encoded and the HTML is rendered as:

    But that's exactly how a valid markup should look like. The ampersand must be encoded when used as an attribute. Don't worry, the browser will perfectly fine understand this url.

    Notice that the following is invalid markup, so you don't want this:

    
    

提交回复
热议问题