asp.net-mvc c# razor url 参数视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6514292/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:17:47  来源:igfitidea点击:

c# razor url parameter from view

asp.net-mvcurlrazor

提问by Shaokan

Why does Request["parameterName"]returns null within the view? I know I can get it from the controller but I have to make a little check in the View. I am using ASP.NET MVC 3.

为什么Request["parameterName"]在视图中返回 null?我知道我可以从控制器获取它,但我必须在视图中进行一些检查。我正在使用 ASP.NET MVC 3。

回答by Wouter Simons

You can use the following:

您可以使用以下内容:

Request.Params["paramName"]

See also: When do Request.Params and Request.Form differ?

另请参阅: Request.Params 和 Request.Form 何时不同?

回答by Daniel

I've found the solution in this thread

在这个线程中找到了解决方案

@(ViewContext.RouteData.Values["parameterName"])

回答by Ravi Ram

@(ViewContext.RouteData.Values["parameterName"])

worked with ROUTE PARAM.

与 ROUTE PARAM 合作。

Request.Params["paramName"]

did not work with ROUTE PARAM.

不适用于 ROUTE PARAM。

回答by Jamie Dixon

If you're doing the check inside the View, put the value in the ViewBag.

如果您在 View 中进行检查,请将值放入ViewBag.

In your controller:

在您的控制器中:

ViewBag["parameterName"] = Request["parameterName"];

It's worth noting that the Requestand Responseproperties are exposed by the Controllerclass. They have the same semantics as HttpRequestand HttpResponse.

值得注意的是,RequestResponse属性由Controller类公开。它们与HttpRequest和具有相同的语义HttpResponse