Asp.NET Core在URL中添加了一个Plus

时间:2017-06-13 15:43:00

标签: c# asp.net asp.net-mvc

当我尝试使用get方法提交表单时,为什么ASP.NET Core会在URI中添加一个Plus(+)?

例如,我有两个字段来计算BMIheightweight。提交表格后,我得到以下网址:
http://localhost:59953/?height=170&weight+=65

在控制器中,我只获取height参数,因为在网址中加权后,有一个+

[HttpGet]
public ActionResult Index(int height, int weight)
{
    // The height is 170 but the weight is 0!
    return View();
}

这是Razor代码形式:

<form method="get">
    <div class="form-group">
        <label for="height">Height in cm</label>
        <input name="height" id="height" class="form-control"/>
    </div>
    <div class="form-group">
        <label for="weight">Weight in kg</label>
        <input name="weight "id="weight" class="form-control"/>
    </div>
    <button class="btn btn-primary" type="submit">Calculate</button>
</form>

1 个答案:

答案 0 :(得分:2)

您的重量属性的name属性中有一个额外的空格,请考虑将其删除:

<input name="weight" id="weight" class="form-control"/>

默认情况下,空格将被编码为&#39; +&#39; URL中的字符,因为&#34; weight +&#34;与#34; weight&#34;相同,ASP.NET无法正确绑定该值。

此外,您可能想要考虑使用POST而不是GET请求,因为您实际上是在向服务器提交数据而不是检索数据,但您的用例可能会有所不同。< / p>