Web-Api 400 BadRequest when search parameter is blank

僤鯓⒐⒋嵵緔 提交于 2020-01-05 11:57:25

问题


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

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