问题
I am using web-api with mvc4
I am making searching functionality, in some cases like if i filter data then remove that textbox value and then press search button, need to show whole listing but in my case showing 400 bad request. as search parameter is blank, i know if search parameter blank then it will throw 400 error with web-api.
any one have proper solution then please let me know.
data: "CurrPage=" + JsCurrPage + "&PageSize=" + parseInt(pagesize) + "&BuildTypeName=" + $("#BuildTypeName").val(),
Here in some cases BuildType is blank. when search made
//controller
public HttpResponseMessage GetBuildTypeList(int CurrPage, int PageSize, string BuildTypeName)
{
}
Net -> XHR URL is :
http://{parentURL}/api/BuildTypeWebApi/GetBuildTypeList?CurrPage=1&PageSize=10&BuildTypeName=
回答1:
If you allow CurrPage and PageSize to be empty, then you need to accept nullable ints:
public HttpResponseMessage GetBuildTypeList(int? CurrPage, int? PageSize, string BuildTypeName)
Then, you'll update the query so it return the entire list if no filter values are provided.
回答2:
public HttpResponseMessage GetBuildTypeList(string BuildTypeName, int CurrPage = 1, int PageSize = 0)
In you business logic you can assume that a PageSize of 0 means all records.
来源:https://stackoverflow.com/questions/18412562/web-api-400-badrequest-when-search-parameter-is-blank