我想判断Razor标记中的URL参数.例如,我该如何做这样的事情:
<div id="wrap" class="@{if (URL "IFRAME" PARAMETER EQUALS 1) iframe-page}">
我想判断Razor标记中的URL参数.例如,我该如何做这样的事情:
<div id="wrap" class="@{if (URL "IFRAME" PARAMETER EQUALS 1) iframe-page}">
<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
.这是我找到的这个景点的清洁之道.