C# 在ASP.NET MVC中使用带有ViewBag的URL(链接)#

C# 在ASP.NET MVC中使用带有ViewBag的URL(链接)#,c#,asp.net-mvc,C#,Asp.net Mvc,我在视图中使用的ViewBag如下所示: ViewBag.Text = "This is the" + @Html.ActionLink("link","action","Home"); 但它不起作用。有什么办法吗?谢谢。您应该使用Html.Raw方法 @{ ViewBag.Text = "This is the" + @Html.ActionLink("link", "Index", "Home"); } @Html.Raw(ViewBag.Text) 如果您只需使用@ViewB

我在视图中使用的ViewBag如下所示:

ViewBag.Text = "This is the" + @Html.ActionLink("link","action","Home");

但它不起作用。有什么办法吗?谢谢。

您应该使用
Html.Raw
方法

@{
    ViewBag.Text = "This is the" + @Html.ActionLink("link", "Index", "Home");
}

@Html.Raw(ViewBag.Text)
如果您只需使用
@ViewBag.Text
,razor将呈现如下内容

This is the<a href="/home/index">link</a>
这是
因为razor在表达式结果前面加上
@


Html.Raw
方法将返回不带Html编码的标记。

您应该使用
Html.Raw
方法

@{
    ViewBag.Text = "This is the" + @Html.ActionLink("link", "Index", "Home");
}

@Html.Raw(ViewBag.Text)
如果您只需使用
@ViewBag.Text
,razor将呈现如下内容

This is the<a href="/home/index">link</a>
这是
因为razor在表达式结果前面加上
@


Html.Raw
方法将返回没有Html编码的标记。

什么不起作用?您期望的行为是什么?我希望有一个工作url,但它显示为文本。什么不工作?您期望的行为是什么?我希望有一个工作url,但它显示为文本。谢谢您的回答!谢谢你的回答!