Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebApi 500 Internal Server Error

I am experimenting with WebApi and have created a controller with two methods.

I started with the following method first:

    [HttpGet]
    [Route("car/{registration}")]
    public object GetCarByRegistration(string registration) {
        return null;
    }

When debugging I put a breakpoint on return null; tested url http://localhost:51245/api/car/yw25jdk which work fine, visual studio stopped at my breakpoint and the registration variable was the same value in the url.

But when I added the following method:

    [HttpGet]
    [Route("car/{serial}")]
    public object GetCarBySerial(string serial) {
        return null;
    }

The first url stopped working and I started to get 500 - Internal Server Error. If I take the second method out then the first method works again.

I cannot understand why the second method breaks the first one.

Can someone explain this to me please?

like image 804
Ommy Avatar asked Mar 15 '23 04:03

Ommy


1 Answers

Well thats because ASP does not know which method to use when you go to the url http://localhost:51245/api/car/yw25jdk since both methods say they expect a string as their parameter.

How should ASP know the difference between /car/{registration} and /car/{serial], since both of them are string?

You should change the Route of on of them, to get it working

like image 74
jollyjoyce1995 Avatar answered Mar 24 '23 12:03

jollyjoyce1995