1

While testing my API in Browser, only the Get method is called.

When I browse http://localhost:xxxxx/api/student/GetStudentById/38 I get the correct value from Database. Here GetStudentById is the [ActionName] under [HttpGet].

But when I browse http://localhost:xxxxx/api/student/StudentDeleteById/38 I get "The requested resource does not support http method 'GET'." (Why it is getting Redirected to Get)? Here StudentDeleteById is the [ActionName] under [HttpDelete].

I have tested my API in Postman every Request is executing successfully(i.e Get, Put, Post, Delete)

Below is my WebApiConfig.cs

config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
1
  • 3
    because if you execute the method in the browser you actually do a get request and not a delete request, but your action is taged with httpdelete, and thats why it gets ignored, and you get an error Commented Apr 22, 2020 at 19:18

1 Answer 1

1

Browser sends only GET requests when you type URL in the address bar. In order to send DELETE request, you can use curl command-line util

curl -X "DELETE"  http://localhost:xxxxx/api/student/StudentDeleteById/38

or create small page with jQuery which sends request:

function sendDelete() {
  $.ajax( 
  {
    url: 'http://localhost/api/student/StudentDeleteById/38',
    method: 'DELETE'
  }).done(function () {
    alert('done');
  });

}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.