locked
Implement Search Action in Web API and Pass a Payload and Get the Data RRS feed

  • Question

  • User-1466455470 posted

    Hi,

    I would like to Implement Search Action in AccountController and Pass a Payload and Get the Data.

    The URl is Suppose to be:

    Account/1/Search/

    Payload:

    {

    "Amount": "500",

    "Date":"12/11/2015"

    }

    Now this request should go to Search Action and do the process and get the data.

    Best Regards,

    Hunaid Hussain.

    Wednesday, February 24, 2016 1:21 PM

Answers

  • User-782957977 posted

    You could easily create rest service with url (Account/1/Search/) using attribute routing -

    http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

        [RoutePrefix("Api/Account")]
        public class AccountSearchController : ApiController
        {
            [HttpPost]
            [Route("{id:int}/Search")]
            public HttpResponseMessage Search(int id, [FromBody] AccountSearchModel model)
            {
    
                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, "value");
                return response;
            }
        }

    Model

    Model
        public class AccountSearchModel
        {
            public int Amount {get;set;}
    
            public string Date {get;set;}
        }

    Client code to rest service

    using (var client = new HttpClient())
                {
                    var model = new {Amount = 100,Date="12/11/2015"};
                    client.BaseAddress = new Uri("http://localhost:58745/");
                    var response = client.PostAsJsonAsync("Api/Account/1/Search", model).Result;
                   
                    if (response.IsSuccessStatusCode)
                    {
                        string responseString = response.Content.ReadAsStringAsync().Result;
                    }
                }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 25, 2016 3:57 AM
  • User36583972 posted

    Hi itshunaid,
    From your description, you can try like the below.
    Firstly, as santhoshje said, you can define a data model and a method to receive, to receive data from the client.

            [AllowAnonymous]
            public Mode1 Put( [FromBody]Mode1 model1)
            {
                Mode1 m1 = new Mode1();
                try {
                    m1.Amount = model1.Amount + 1;
                    m1.Date = model1.Date;
                }catch
                {
                    m1.Amount = 0;
                    m1.Date = "error";
                }
                return m1;
            }

    Secondly, you can send and receive JSON data on the client through Ajax.

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="Scripts/jquery-1.10.2.js"></script>
        <script>
            $(document).ready(function () {
                $('#btnmethod').click(function () {
                    var id = 1;
                    $.ajax({
                        url: "http://localhost:21258/api/values",
                        type: "Put",
                        contentType: "application/json; charset=urf-8",
                        dataType: "json",
                        data: "{ Amount:500,Date:'12/11/2015'}",
                        success: function (response) {
                            alert("OK-Put" + JSON.stringify(response));
                        }, 
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert("error-Put:" + textStatus + "  " + errorThrown);
                        }
                    });
                });
            });
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="TextBox1" runat="server" Height="31px" Width="417px"></asp:TextBox>
            <br />
            <asp:Button ID="btnmethod" runat="server" Text="Method" />
            <br />
            <br />
            <div>
            </div>
            <br />
        </div>
        </form>
    </body>
    </html>

    Icon:

    Best Regards,


    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 25, 2016 5:00 AM

All replies

  • User-782957977 posted

    You could easily create rest service with url (Account/1/Search/) using attribute routing -

    http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

        [RoutePrefix("Api/Account")]
        public class AccountSearchController : ApiController
        {
            [HttpPost]
            [Route("{id:int}/Search")]
            public HttpResponseMessage Search(int id, [FromBody] AccountSearchModel model)
            {
    
                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, "value");
                return response;
            }
        }

    Model

    Model
        public class AccountSearchModel
        {
            public int Amount {get;set;}
    
            public string Date {get;set;}
        }

    Client code to rest service

    using (var client = new HttpClient())
                {
                    var model = new {Amount = 100,Date="12/11/2015"};
                    client.BaseAddress = new Uri("http://localhost:58745/");
                    var response = client.PostAsJsonAsync("Api/Account/1/Search", model).Result;
                   
                    if (response.IsSuccessStatusCode)
                    {
                        string responseString = response.Content.ReadAsStringAsync().Result;
                    }
                }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 25, 2016 3:57 AM
  • User36583972 posted

    Hi itshunaid,
    From your description, you can try like the below.
    Firstly, as santhoshje said, you can define a data model and a method to receive, to receive data from the client.

            [AllowAnonymous]
            public Mode1 Put( [FromBody]Mode1 model1)
            {
                Mode1 m1 = new Mode1();
                try {
                    m1.Amount = model1.Amount + 1;
                    m1.Date = model1.Date;
                }catch
                {
                    m1.Amount = 0;
                    m1.Date = "error";
                }
                return m1;
            }

    Secondly, you can send and receive JSON data on the client through Ajax.

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="Scripts/jquery-1.10.2.js"></script>
        <script>
            $(document).ready(function () {
                $('#btnmethod').click(function () {
                    var id = 1;
                    $.ajax({
                        url: "http://localhost:21258/api/values",
                        type: "Put",
                        contentType: "application/json; charset=urf-8",
                        dataType: "json",
                        data: "{ Amount:500,Date:'12/11/2015'}",
                        success: function (response) {
                            alert("OK-Put" + JSON.stringify(response));
                        }, 
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert("error-Put:" + textStatus + "  " + errorThrown);
                        }
                    });
                });
            });
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="TextBox1" runat="server" Height="31px" Width="417px"></asp:TextBox>
            <br />
            <asp:Button ID="btnmethod" runat="server" Text="Method" />
            <br />
            <br />
            <div>
            </div>
            <br />
        </div>
        </form>
    </body>
    </html>

    Icon:

    Best Regards,


    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 25, 2016 5:00 AM
  • User-1466455470 posted

    Thanks Santosh!

    Thursday, February 25, 2016 5:29 AM
  • User-1466455470 posted

    Thanks Yohann!

    Thursday, February 25, 2016 5:30 AM