user4596412
user4596412

Reputation: 11

.NET Core API is giving 404 from angular app

I'm trying to call the method in the controller by URL, but I getting a 404 error. What am I doing wrong? API End Pint: http://localhost:5000/Home/HomeTest

/*.net core web-api*/
namespace MyApp.Controllers
{
    Route("api/[controller]")]
    [ApiController]
    public class HomeController : ControllerBase
    {
        public HomeController()
        {}

        [Route("api/[controller]/[action]")]
        [HttpGet]
        public string HomeTest()
        {
            return "123";
        }
}
/*angular*/
export class HomeComponent implements OnInit {
    constructor(private http: HttpClient) {}
    ngOnInit()
    {
        let test = "";
        test = "http://localhost:5000/api/Home/HomeTest";
        this.http.get(test).subscribe((data: any) => console.log(data), (err: any) => console.log(err));
    }
}

Upvotes: 1

Views: 1939

Answers (1)

Xueli Chen
Xueli Chen

Reputation: 12725

For the Route attribute confifuration in your Controller, the url called in the request should be http://localhost:5000/api/Home/api/Home/HomeTest.

Or you could change the [Route("api/[controller]/[action]")] on the action with [Route("/api/[controller]/[action]")], and then you could call the request with url http://localhost:5000/api/Home/HomeTest.

Refer to Combining attribute routes for more details about the Route attribute.

Upvotes: 2

Related Questions