我想判断Razor标记中的URL参数.例如,我该如何做这样的事情:

<div id="wrap" class="@{if (URL "IFRAME" PARAMETER EQUALS 1) iframe-page}">

推荐答案

Similar thread

<div id="wrap" class=' @(ViewContext.RouteData.Values["iframe"] == 1 ? /*do sth*/ : /*do sth else*/')> </div>

EDIT 01-10-2014: Since this question is so popular this answer has been improved.

The example above will only get the values from RouteData, so only from the querystrings which are caught by some registered route. To get the querystring value you have to get to the current HttpRequest. Fastest way is by calling (as TruMan pointed out) `Request.Querystring' so the answer should be:

<div id="wrap" class=' @(Request.QueryString["iframe"] == 1 ? /*do sth*/ : /*do sth else*/')> </div>

你也可以查RouteValues vs QueryString MVC?

EDIT 03-05-2019: Above solution is working for .NET Framework.
As others pointed out if you would like to get query string value in .NET Core you have to use Query object from Context.Request path. So it would be:

<div id="wrap" class=' @(Context.Request.Query["iframe"] == new StringValues("1") ? /*do sth*/ : /*do sth else*/')> </div>

请注意,我在语句中使用了StringValues("1"),因为Query返回StringValues struct ,而不是纯string.这是我找到的这个景点的清洁之道.

Asp.net相关问答推荐

有人可以解释一下这个Eager 加载示例吗?

DBSet 不包含 Where 的定义

您如何处理多个环境的多个 web.config 文件?

如何在现有数据库中创建 ASP.Net Identity 表?

ASP.NET:获取自 1970 年 1 月 1 日以来的毫秒数

如何从 asp:Repeater 循环遍历项目模板中的项目?

如何在 ASP.NET core rc2 中禁用浏览器缓存?

带有邮箱地址参数的 WebApi 方法从 HttpClient 返回 404

排序下拉列表? - C#,ASP.NET

如何在 ASP.NET MVC3 中配置区域

如何在新选项卡中打开 asp:HyperLink.NavigateUrl

将数据表导出到 Excel 文件

跟踪点有什么用途?

无法共同创建探查器错误 - 但未使用探查器

使用 ASP.NET Web API 对 PUT 和 DELETE 的 CORS 支持

要调用此方法,Membership.Provider属性必须是ExtendedMembershipProvider的实例

使用 .NET 连接到 AS400

URL 路由、图像处理程序和潜在危险的 Request.Path 值

使用 NLog 在 ASP.NET Web API 2.1 中进行全局异常处理?

~/ 等价于 javascript