Ap put request

osyris 236 Reputation points
2021-08-23T13:03:28.127+00:00

I dont know what is going on on this website.
But everytime i try to place a question here i get "Acces denied" Message on this website.

its very annoying please fix it.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,400 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,417 questions
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 61,731 Reputation points
    2021-08-23T18:51:36.35+00:00

    this is pretty poorly written, no wonder you have trouble.

    You should either post json, or do a form post, not both. the server put method does not support form posts, so if a file is included it will not work.

    also, why a single space for nulls?

    assuming the hook values are bound to form elements they would all be string, but your code uses them as others data types.

    your code sets a space to for null numbers, but the server gets a binding error when this happens, as a space can not map to a number.

    it looks like you are using an entity for a form post. bad design.

    it looks like edit is react component, but components should not be async. the async calls should be in a useEffect() callback.

    1 person found this answer helpful.
    0 comments No comments

  2. osyris 236 Reputation points
    2021-08-23T13:05:27.023+00:00

    My actual question:

    I am trying to create a PUT request system i have work on it for a couple of hours to make it work but it has been a complete drama, sometimes the code work and Allot of times it completly fails. I feel like this could be code way better and more efficient especially from the client side
    the idea is that it should only update the fields that are not empty.

    C#:

    [HttpPut("{id}")]
                public async Task<IActionResult> EditProduct(int id, ProductDto dto)
                {
                    var find = await _dbContext.Products.FindAsync(id);
    
                    if (find == null)
                        return BadRequest("Could not find product");
    
                    if(dto.File != null)
                    {
                        if (dto.File.Length > 0)
                        {
    
                            string root = _env.WebRootPath + "/client/src/images/ProductImages/";
                            string oldFileName = find.ImagePath;
    
                            if (System.IO.File.Exists(Path.Combine(root, oldFileName)))
                                System.IO.File.Delete(Path.Combine(root, oldFileName));
    
    
                            string filename = Guid.NewGuid().ToString() + "_" + dto.File.FileName;
                            string filepath = Path.Combine(root, filename);
    
                            using (var stream = System.IO.File.Create(filepath))
                            {
                                await dto.File.CopyToAsync(stream);
                            }
    
                            find.ImagePath = filename;
                        }
                    }
                    if(!string.IsNullOrWhiteSpace(dto.Name)){ find.Name = dto.Name; }
                    if(!string.IsNullOrWhiteSpace(dto.Description)){ find.Description = dto.Description; }
                    if(dto.Price >= 0){ find.Price = dto.Price; }
                    if (!string.IsNullOrWhiteSpace(dto.Category)) { find.Category = dto.Category; }
    
                    await _dbContext.SaveChangesAsync();
    
                    return Ok();
                }
    
    0 comments No comments

  3. osyris 236 Reputation points
    2021-08-23T13:06:21.147+00:00

    Client side Reactjs :

    const edit = async () =>{
            const[status,setStatus] = useState("");
            const[name,setName] = useState(null);
            const[description,setDescription]= useState(null);
            const[price,setPrice] = useState(null);
            const[category,setCategory] = useState(null);
            const[file, setFile] = useState(null);
            const inputRef2 = useRef(null);
    
        const edit = async () =>{
            const formdata = new FormData();
    
        let senddata = formdata
    
            if(file !== null){ formdata.append("file",file, file.name);  }
            if(name === null){formdata.append("name", " ");}
            if(description === null){formdata.append('description'," ");}
            if(price === null){formdata.append('price'," ");}
            if(category === null){formdata.append('category'," ");}
    
        let object = {
            name: name,
            description : description,
            price: price,
            category: category,
            file: null
        }
    
        if(name === null){ object.name = " ";}
        if(description === null){ object.description = " ";}
        if(price === null){ object.price = -1;}
        if(category === null){ object.category = " ";}
    
          if(file === null){  senddata = object; }
    
          console.log(senddata)
    
          const res = await  axios.put(url,senddata)
            if(res.status === 200) {
                setStatus("Product has been succesfuly updated") }
            else{
                setStatus("Product has Failed to update")  }
    

  4. osyris 236 Reputation points
    2021-08-23T19:47:14.297+00:00

    This was not the full code , every time i post the full script i get a error on this website.
    also I have not finished the design

    I am trying to upload the Code to that part but im not even allowed:

        **Access Denied  
    

    You don't have permission to access "http://learn.microsoft.com/answers/answers/524392/post.html" on this server.
    Reference #18.b5361602.1629747933.2f50a30a**

    0 comments No comments

  5. osyris 236 Reputation points
    2021-08-23T19:49:14.42+00:00

    Only way i can show the rest of the code:

    125727-code.png

    0 comments No comments