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

↘锁芯ラ 提交于 2019-12-13 11:52:43

问题


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

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:

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.


回答1:


Use the following code:

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

        ...
    }



回答2:


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.Params rather than putting it as an explicit parameter



来源:https://stackoverflow.com/questions/21832610/mvc-how-to-get-parameter-value-from-get-request-which-has-parameter-names-inclu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!