如何从MVC剃刀标记查询字符串参数? [英] How to get query string parameter from MVC Razor markup?

查看:137
本文介绍了如何从MVC剃刀标记查询字符串参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要查询的网址参数在我的剃刀标记。例如,我怎么做这样的事情:

 < D​​IV ID =包装类=@ {如果(URLIFRAME参数等于1)的iframe页面}>


解决方案

<一个href=\"http://stackoverflow.com/questions/3660329/in-asp-net-mvc-2-how-do-i-get-route-values-into-my-navigation-controller-so-i\">Similar螺纹

 &LT; D​​IV ID =包装类='@(ViewContext.RouteData.Values​​ [IFRAME] == 1 / *做某事* / / *做某事?其他* /)&GT; &LT; / DIV&GT;

修改2014年1月10日:
由于这个问题是如此受欢迎,这个答案得到了提高。

上面的例子只会从的RouteData 的值,所以仅从这是由一些注册航线抓住了查询字符串。要获取查询字符串值,你必须去的电流的Htt prequest 。最快的方法是调用(如杜鲁门指出的)'的Request.QueryString所以答案应该是:

 &LT; D​​IV ID =包装类='@(的Request.QueryString [IFRAME] == 1 / *做某事* /?/ *做某事*其他/')&GT; &LT; / DIV&GT;

您也可以检查<一href=\"http://stackoverflow.com/questions/14090546/routevalues-vs-querystring-mvc/14092478#14092478\">RouteValues VS查询字符串MVC?

I want to check the URL parameter in my Razor markup. For example, how do I do something like this:

<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>

You can also check RouteValues vs QueryString MVC?

这篇关于如何从MVC剃刀标记查询字符串参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆