Skip to content
Loading
How to fetch web api using react.js component? Urgent
  • Thanks. Would you please provide the complete react.js code to call api and get the logged in username value and set the username value in label and render the value.
     
    Please help.  I put the code in the zip file in the original post.Also, thanks for pointing the mistake in Controller api.
  • Ganesan C
    Hi, 
     
    You have added route parameterfor UserController is [Route("[controller]")]. But you have called "api/User/" from React component. So, Your route should be [Route("api/[controller]")], then only it call correct User controller.
    1. [ApiController]  
    2. [Route("api/[controller]")]  
    3. public class UserController: ControllerBase  
    4. {  
    5.   
    6. [HttpGet]  
    7. public string GetLogonUserId()  
    8. {  
    9.   
    10. }  
    11. }  
    1. componentDiDMount() {  
    2. fetch(‘api/User/ GetLogonUserId’)  
    3. .then ((response)=> response.json())  
    4.     
    5.   
    6. }