asp.net-mvc MVC-如何从具有包含点字符的参数名称的获取请求中获取参数值

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

MVC- How to get parameter value from get request which has parameter names including dot characters

asp.net-mvcurlparametersgetasp.net-mvc-routing

提问by nooaa

In MVC, I know we can get parameters from a get request like this:

在 MVC 中,我知道我们可以从这样的 get 请求中获取参数:

Request:

要求:

http://www.example.com/method?param1=good&param2=bad

And in controller

并在控制器中

public ActionResult method(string param1, string param2)
{
   ....
}

But in my situation an external website sends me a get request like:

但在我的情况下,外部网站向我发送了一个 get 请求,例如:

http://www.example.com/method?param.1=good&param.2=bad

And in controller when i try to meet this request like as follow:

在控制器中,当我尝试满足此请求时,如下所示:

public ActionResult method(string param.1, string param.2)
{
   ....
}

I get build errors because of dot in variable name. How can i get these parameters ? Unfortunately i can not ask them to change parameter names.

由于变量名称中的点,我收到构建错误。我怎样才能得到这些参数?不幸的是,我不能要求他们更改参数名称。

回答by ssimeonov

Use the following code:

使用以下代码:

    public ActionResult method()
    {
        string param1 = this.Request.QueryString["param.1"];
        string param2 = this.Request.QueryString["param.2"];

        ...
    }

回答by James Haug

This will probably be your best bet:

这可能是你最好的选择:

/// <summary>
/// <paramref name="param.1"/>
/// </summary>
public void Test1()
{
    var value = HttpContext.Request.Params.Get("param.1");
}

Get the parameter from HttpContext.Request.Paramsrather than putting it as an explicit parameter

从中获取参数HttpContext.Request.Params而不是将其作为显式参数