2017-06-13 14 views
0

當我嘗試使用get方法提交表單時,爲什麼ASP.NET Core會在URI中添加一個Plus(+)?Asp.NET Core在URL中添加了一個Plus

例如我有兩個字段來計算BMIheightweight。提交表格後,我得到以下網址:
http://localhost:59953/?height=170&weight+=65

在控制器中,我只得到height參數,因爲在URL中的權重後,有一個+

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

這是形式的剃刀代碼:

<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

URL中查詢部分的空間被編碼爲加號 – Jamiec

回答

2

你有name屬性爲你的體重物業內的額外空間,考慮將其刪除:

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

默認情況下,空間將在URL中被編碼爲'+'字符,並且由於「weight +」與「weight」不同,ASP.NET將無法正確綁定該值。

此外,您可能想要考慮使用POST而不是GET請求,因爲您實際上將數據提交給服務器而不是檢索它,但您的用例可能會有所不同。

+0

嗨。多麼失敗。我正在使用GET,因爲我可以看到問題出在哪裏。我想,也許這是一個只採取第一個參數的慣例:)謝謝! –

相關問題