locked
Http request delete RRS feed

  • Question

  • User-148788041 posted
    Hi
    I have url
    Input in url like this

    http://API/emp/55
    Or
    http://API/emp?id=67
    How to do in httpresponsemessage
    Tuesday, March 5, 2019 12:42 PM

All replies

  • User475983607 posted

    Hi
    I have url
    Input in url like this

    http://API/emp/55
    Or
    http://API/emp?id=67
    How to do in httpresponsemessage

    The first URL is a route parameter used in MVC and Web API.  The the second is query string.

    This is a fundamental concept.   Over the last several months, the community has tried many times to explain the fundamentals.  Yet, you continue to ask the same basic questions over and over.  I recommend that you take a class or find a way to set aside time to educate yourself.  Otherwise we cannot answer your question because clearly you do not understand the answers.  

    Tuesday, March 5, 2019 12:58 PM
  • User753101303 posted

    Hi,

    Not 100% clear. It seems you are looking for https://www.tutorialsteacher.com/webapi/implement-delete-method-in-web-api

    I would not return this as part of the response as you just deleted the resource (so anyway you would have a 404 if trying to use the url). It makes sense when CREATING a resource so that someone consuming your API knows which url to use to retrieve the newly created resource.

    If it doesn't help please be a bit more explicit (for now my understanding is that you are trying to send back the location for the resource you just deleted as part of a DELETE request ??? but it's really rather a guess).

    Tuesday, March 5, 2019 1:00 PM
  • User-148788041 posted
    I want to consume this webapi and delete record.
    Tuesday, March 5, 2019 1:30 PM
  • User475983607 posted

    I want to consume this webapi and delete record.

    If this is a programming problem, post source code that reproduces the problem.  Explain the expected results and actual results,

    Otherwise the openly published Web API documentation covers this subject extensively.

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

    Tuesday, March 5, 2019 1:54 PM
  • User753101303 posted

    From JavaScript or from C# ?

    Tuesday, March 5, 2019 4:29 PM
  • User36583972 posted

    anth,

    Hi
    I have url
    Input in url like this

    http://API/emp/55
    Or
    http://API/emp?id=67
    How to do in httpresponsemessage

    The following sample for your reference.

    WebApiConfig:

                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );

    Controller:

            // DELETE api/values/67
            // DELETE api/values?id=67
            public void Delete(int id)
            {
                Console.WriteLine(id);
    
            }

    Method:

    using (var client = new System.Net.Http.HttpClient())
                {
                    var baseUrl = Request.Url.GetLeftPart(UriPartial.Authority);
                    client.BaseAddress = new Uri(baseUrl);
                    //var response = client.DeleteAsync("/api/Values/67").Result;
                    var response = client.DeleteAsync("/api/Values?id=67").Result;
                    string s = await response.Content.ReadAsStringAsync();
                }
    

    Best Regards

    Yong Lu

    Wednesday, March 6, 2019 2:34 AM
  • User-148788041 posted

    will int id get values from httpRequest  ?id=67  and api/values/67 

    Will it work for both?

    Wednesday, March 6, 2019 2:43 AM
  • User475983607 posted
    Yes, it really does help if you read the linked docs as it explains the details.
    Wednesday, March 6, 2019 2:55 AM
  • User-148788041 posted
    Httpresponsemessage res;
    res= client.deleteasync(/API/employee/1066).result
    500 internal error
    Wednesday, March 6, 2019 5:27 AM
  • User753101303 posted

    500 internal error

    Hi,

    It means you have a server side exception more likely in your controller. By default you should find error details in the Windows event log which should allow to understand which error you have and then how to fix this error.

    Wednesday, March 6, 2019 11:53 AM