osyris zosar

osyris zosar

  • NA
  • 289
  • 27k

querystring parameters are not working in API

Jun 2 2021 4:30 PM
I am trying to connect the Front-end(reactjs) with the asp net core API

Front-end
  1. async getFilterData() {  
  2.        this.setState({ tableLoading: true });  
  3.   
  4.          
  5.        const   TitleFIlter = document.getElementById("TitleFIlter").value  
  6.        const   descriptionFilter = document.getElementById("DescriptionFilter").value  
  7.        const priceminFIlter = document.getElementById("PriceMin").value   
  8.        const pricemaxFIlter = document.getElementById("PriceMax").value   
  9.        console.log(" title =" + TitleFIlter);  
  10.        console.log("description = " + descriptionFilter);  
  11.        console.log(" PriceMin =" + priceminFIlter);  
  12.        console.log("PriceMax = " + pricemaxFIlter);  
  13.   
  14.        const url = `api/upload/${TitleFIlter}/${descriptionFilter}/${priceminFIlter}/${pricemaxFIlter}`  
  15.        const response = await axios.get(url)  
  16.        const data = await response.data;  
  17.        this.setState({  
  18.            allproducts: data,  
  19.            tableLoading: false  
  20.        });  
  21.        console.log(data);  
  22.    }  
 this is my backend(asp net core API):

  1. HttpGet]  
  2.         [Route("{Title?}/{Description?}/{Pricemin?}/{Pricemax?}")]  
  3.         public async Task<List<Product>> GetFilter([FromQuery]  
  4.          string Title,  
  5.          string  Description,  
  6.             int PriceMin,  
  7.             int PriceMax   
  8.             )  
  9.         {  
  10.             IQueryable<Product> products = _context.products;  
  11.   
  12.   
  13.             if (!string.IsNullOrEmpty(Title))  
  14.                 products = products.Where(x => x.Title.ToLower().Contains(Title.ToLower()));  
  15.   
  16.             if (!string.IsNullOrEmpty(Description))  
  17.                 products = products.Where(x => x.Description.ToLower().Contains(Description.ToLower()));  
  18.   
  19.             if (PriceMin > 0)  
  20.                 products = products.Where(x => x.Price >= PriceMin);  
  21.   
  22.             if (PriceMax > 0)  
  23.                 products = products.Where(x => x.Price <= PriceMax);  
  24.   
  25.             return await products.ToListAsync();  
  26.         }  
Anytime I dont fill in all the filter inputs I get a 404 error
How do i solve this 

Answers (4)