Search code examples
asp.net-web-apibad-requesthttp-status-code-400

Ajax Web-Api optional parameter null issue.


Ajax webapi when parameter is null or blank then 400 bad reuqsest occurs. solution needed asap.

http://{parenturl}/api/BuildTypeWebApi/GetBuildTypeList?CurrPage=1&PageSize=10&BuildTypeName=

here BuildTypeName is optional parameter when there is not search parameter passed how to reduce 400 error.

//controller

public HttpResponseMessage GetBuildTypeList(int CurrPage, int PageSize, string BuildTypeName = "")
{
}

here issue with only BuildType.

help some one.

Regards


Solution

  • You need to change the way the request is made. Either complete your request string by adding ="" to the end, or leave out the BuildTypeName parameter when it is empty.

    So you get either of these two cases:

    /api/BuildTypeWebApi/GetBuildTypeList?CurrPage=1&PageSize=10&BuildTypeName=""

    /api/BuildTypeWebApi/GetBuildTypeList?CurrPage=1&PageSize=10

    This way, Web API actually knows what you want to do with the BuildTypeName parameter. In your case it was an incomplete request.