'i have error in my get method its show me 404 error .net core and angular
The problem when i send request to api in my api my post methd and getAllUsers its work but the GetUser method doesn't work its show me 404 error i was trying to solve it i used many route but still this problem so if any one can help me to solve this problem this is my api code:
[EnableCors("MyPolicy")]
[Route("api/Login")]
[ApiController]
public class LoginController : ControllerBase
{
[EnableCors("MyPolicy")]
[HttpGet]
public IEnumerable<User> GetAllUsers()
{
IUserRepository obj = new UserRepository();
return obj.GetAll();
}
[Route("api/Login/GetUser/{id?}")]
[HttpGet]
public User GetUser(int id)
{
IUserRepository obj = new UserRepository();
return obj.Get(id);
}
[HttpPost]
public User PostAddUser(User item)
{
IUserRepository obj = new UserRepository();
return obj.Add(item);
}
and this is my service in angular :
Load(id:number){
debugger;
return this.httpcli.get("https://localhost:44366/api/Login/GetUser?id="+id);
}
and this my startup.cs in core api :
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options => options.AddPolicy(name: "MyPolicy", builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()));
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "LoginApi", Version = "v1" });
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseHttpsRedirection();
app.UseRouting();
app.UseCors("MyPolicy");
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
Solution 1:[1]
You can try to send a path parameter instead of a request parameter:
Load(id:number){
debugger;
return this.httpcli.get(`https://localhost:44366/api/Login/GetUser/${id}`);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | OLO |
