C# 如何从 MVC Razor 标记中获取查询字符串参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11248064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get query string parameter from MVC Razor markup?
提问by TruMan1
I want to check the URL parameter in my Razor markup. For example, how do I do something like this:
我想检查 Razor 标记中的 URL 参数。例如,我该如何做这样的事情:
<div id="wrap" class="@{if (URL "IFRAME" PARAMETER EQUALS 1) iframe-page}">
采纳答案by Mariusz
<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.
编辑 01-10-2014:由于这个问题如此受欢迎,所以这个答案已经得到改进。
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:
上面的示例只会从 中获取值RouteData,因此只能从某个注册路由捕获的查询字符串中获取值。要获取查询字符串值,您必须获得当前的HttpRequest. 最快的方法是调用(正如 TruMan 指出的)`Request.Querystring' 所以答案应该是:
<div id="wrap" class=' @(Request.QueryString["iframe"] == 1 ? /*do sth*/ : /*do sth else*/')> </div>
You can also check RouteValues vs QueryString MVC?
您还可以检查RouteValues 还是 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 Coreyou have to use Queryobject from Context.Requestpath. So it would be:
编辑 03-05-2019:上述解决方案适用于.NET Framework。
正如其他人指出的那样,如果您想在.NET Core 中获取查询字符串值,则必须使用路径中的Query对象Context.Request。所以它会是:
<div id="wrap" class=' @(Context.Request.Query["iframe"] == new StringValues("1") ? /*do sth*/ : /*do sth else*/')> </div>
Please notice I am using StringValues("1")in the statement because Queryreturns StringValuesstruct instead of pure string. That's cleanes way for this scenerio which I've found.
请注意我StringValues("1")在语句中使用,因为Query返回StringValuesstruct 而不是 pure string。这是我发现的这个场景的清洁方式。
回答by YavgenyP
I think a more elegant solution is to use the controller and the ViewData dictionary:
我认为更优雅的解决方案是使用控制器和 ViewData 字典:
//Controller:
public ActionResult Action(int IFRAME)
{
ViewData["IsIframe"] = IFRAME == 1;
return View();
}
//view
@{
string classToUse = (bool)ViewData["IsIframe"] ? "iframe-page" : "";
<div id="wrap" class='@classToUse'></div>
}
回答by Florian Schaal
If you are using .net core 2.0 this would be:
如果您使用的是 .net core 2.0,这将是:
Context.Request.Query["id"]
Sample usage:
示例用法:
<a href="@Url.Action("Query",new {parm1=Context.Request.Query["queryparm1"]})">GO</a>
回答by x5657
It was suggested to post this as an answer, because some other answers are giving errors like 'The name Context does not exist in the current context'.
建议将此作为答案发布,因为其他一些答案给出了诸如“当前上下文中不存在名称上下文”之类的错误。
Just using the following works:
只需使用以下作品:
Request.Query["queryparm1"]
Sample usage:
示例用法:
<a href="@Url.Action("Query",new {parm1=Request.Query["queryparm1"]})">GO</a>
回答by Rolando Retana
Noneof the answers worked for me, I was getting "'HttpRequestBase' does not contain a definition for 'Query'", but this did work:
没有一个答案对我有用,我得到“'HttpRequestBase'不包含'查询'的定义”,但这确实有效:
HttpContext.Current.Request.QueryString["index"]
回答by Pradip Rupareliya
For Asp.net Core 2
对于 Asp.net Core 2
ViewContext.ModelState["id"].AttemptedValue

