Razor默认对字符串进行编码。没有编码的渲染有什么特殊的语法吗?
当前回答
你也可以使用WriteLiteral方法
其他回答
以及已经提到的@Html.Raw(字符串)方法,如果你输出一个MvcHtmlString,它将不会被编码。在向HtmlHelper添加自己的扩展时,或者从视图模型返回您知道可能包含html的值时,这可能很有用。
例如,如果你的视图模型是:
public class SampleViewModel
{
public string SampleString { get; set; }
public MvcHtmlString SampleHtmlString { get; set; }
}
对于Core 1.0+(和MVC 5+)使用HtmlString
public class SampleViewModel
{
public string SampleString { get; set; }
public HtmlString SampleHtmlString { get; set; }
}
then
<!-- this will be encoded -->
<div>@Model.SampleString</div>
<!-- this will not be encoded -->
<div>@Html.Raw(Model.SampleString)</div>
<!-- this will not be encoded either -->
<div>@Model.SampleHtmlString</div>
你也可以使用WriteLiteral方法
对于ActionLink,它通常使用HttpUtility。对链接文本进行编码。 这样的话 你可以使用 HttpUtility.HtmlDecode (myString) 它为我工作时使用HtmlActionLink解码字符串,我想传递。例如:
@Html ActionLink (HttpUtility . HtmlDecode(“myString”、“ActionName……)
因为ASP。NET MVC 3,你可以使用:
@Html.Raw(myString)
@(new HtmlString(myString))
推荐文章
- dynamic (c# 4)和var之间的区别是什么?
- Visual Studio: ContextSwitchDeadlock
- 返回文件在ASP。Net Core Web API
- 自定义HttpClient请求头
- 无法启动IIS Express Web服务器,注册URL失败,访问被拒绝
- 如果我使用OWIN Startup.cs类并将所有配置移动到那里,我是否需要一个Global.asax.cs文件?
- ASP。NET Identity DbContext混淆
- VS2013外部构建错误"error MSB4019: The imported project <path> was not found"
- 从另一个列表id中排序一个列表
- 等待一个无效的异步方法
- 无法加载文件或程序集…参数不正确
- c#中枚举中的方法
- 如何从字符串中删除新的行字符?
- 如何设置一个默认值与Html.TextBoxFor?
- 检查属性是否有属性