locked
API Callback on URL hit RRS feed

  • Question

  • User-614943948 posted

    Just like the way we have Windows Callback functions, that are called by Windows Operating System. Is there a way IIS Server can call an API if a certain URL is hit.

    For example. 

    I have an ASP.NET application. Which has a customer module. What I want is when I delete something from the customer module the API should be called.

    Example http://www.myproject.com/Customer?ID=23;Delete

    When this URL is hit with the query string a certain API call should be made. How can I achieve this?

    Tuesday, June 6, 2017 9:15 AM

Answers

  • User1967761114 posted

    Hi maverick786us,

    According to your description, I suggest you could use API to implement this feature.

    I think the trigger is so difficult to maintain for your other team members.

    So in your case, I think that’s better for send a WebRequest to your API server to delete reward record after the customer deleted.   

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 7, 2017 6:49 AM

All replies

  • User1967761114 posted

    Hi maverick786us,

    According to your description, I could introduction 2 cases for you to implement this feature.

    (1) Use [Route] Attribute on the API method such as the following code.

    public class CustomerController : ApiController
    {
        [HttpGet]
        [Route("customer/{id};delete")]
        public string Delete(int id)
        {
            return "Delete Customer ID=" + id.ToString();
        }
    }

    Then you could access the API by the following URL, that’s similar as your said.

    http://www.myproject.com/customer/1;delete

    The API will return “Delete Customer ID=1”.

     

    (2) Use HttpMethod, see the following code:

    public class CustomerController : ApiController
    {
        [HttpDelete]
        [Route("customer/{id}")]
        public string Delete(int id)
        {
            return "Delete Customer ID=" + id.ToString();
        }
    }
    

    In javascript, you could use the following code to invoke this API:

    $.ajax({
           url: "http://www.myproject.com/customer/1",
           type: "Delete",
           success: function (result) {
               alert(result);
           }
    })

    In .NET, you could use the following code to invoke this API:

    var webRequest = (HttpWebRequest)WebRequest.Create("http://www.myproject.com/customer/1");
    webRequest.Method = "Delete";
    using (var response = (HttpWebResponse)webRequest.GetResponse())
    {
        using (var httpStreamReader = new StreamReader(response.GetResponseStream()))
        {
            var result = httpStreamReader.ReadToEnd();
        }
    }
    

    Note: if you has some issue about the cross-origin(I think you will happen it), you could refer to the following link to enable CORS.

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/enabling-cross-origin-requests-in-web-api

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even

    Wednesday, June 7, 2017 6:04 AM
  • User-614943948 posted

    Thanks Eve for the initial help. Let me explain my requirement with a real live example

    I have 2 ASP.NET projects.

    1) I have an existing ASP.NET application that has customer module, which creates, modify and delete customer. The customer table is linked with an order table, which is the transaction.

    2) Now I am creating a new application that that will use the same database. In that database I have another table for reward points. these reward points are mapped with the customer and order.

    Now I want this whole process to run in such a way that from my first application whenever an order is deleted, it should remove its corresponding value from the rewards points table too, WITHOUT!!! making a single link change with in my first ASP.NET application. 

    So there are 2 ways which I found feasible to achieve this..

    1) Triggers: I can create a trigger that will fire whenever any record is deleted from the order transaction table, and will delete its corresponding entry from the Reward Point table.
    2) API: Call that whenever a delete event is fired, the API will trace it and delete the corresponding entry from the reward table.

    So which one of these approaches should I follow?

    Wednesday, June 7, 2017 6:23 AM
  • User1967761114 posted

    Hi maverick786us,

    According to your description, I suggest you could use API to implement this feature.

    I think the trigger is so difficult to maintain for your other team members.

    So in your case, I think that’s better for send a WebRequest to your API server to delete reward record after the customer deleted.   

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 7, 2017 6:49 AM
  • User-614943948 posted

    Thanks.

    Why are SQL triggers difficult to maintain? Do triggers have some kind of side effect?

    Wednesday, June 7, 2017 6:55 AM
  • User1967761114 posted

    Hi maverick786us,

    That’s just my personal opinion.

    I think that’s so difficult to find trigger when you couldn’t tell other team member, and also SQL statements is so difficult to read without comments.

    I find there has a thread that you had been post~

    https://forums.asp.net/t/2122839.aspx

    It seems like that you couldn’t change any code on the original module?I will help you to find way to resolve it.

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even

    Wednesday, June 7, 2017 7:21 AM
  • User-614943948 posted

    Do you use skype?

    Wednesday, June 7, 2017 8:32 AM