Friday 26 March 2021

Call API using ajax in asp.net bearer token bassed

//////////////////////////// Call Token Authontication

function CallAPI_Authoticate(objvr) {

                              var objvr = {
                                    grant_type: 'password',
                                    username: $("[id$=UserName]").val(),
                                    password: $("[id$=Password]").val(),
                                    scope: $('[id$=AnyOtherParameter]').val()
                                };

                        $.ajax({
                            type: "POST",
                            url: "BsaeURL/"+"Token",
                            headers: { "Accept": "application/json" },
                            contentType: "application/x-www-form-url; charset=urf-8",
                            data: objvr,
                            async: false,
                            success: function (result) {                              
                                if (result != "Error")
                                    CallAPI_Authorized(result);
                            },
                            error: function (req, status, error) {
                            
                                    Show_Alert_Msg(JSON.parse(req.responseText).error_description);                            
                            }
                        });
                    }


//////////////////////////// After Authontication API data comming in JSON

function CallAPI_Authorized(token) {                      
                        var tokes = token.access_token;
                        var Baseulr= "http://localhost:60005"
                        var APIURL= "/api/data/GetEmployees";
                        $.ajax({
                            type: "GET",
                            url: "BaseUrl" + "APIURL",
                            async: false,
                            headers: {
                                Authorization: 'Bearer ' + tokes
                            },
                            dataType: 'json',
                            success: function (result, status, xhr) {
                              
                                Other working code here....
                            },
                            error: function (req, status, error) {
                                Show_Alert_Msg(JSON.parse(req.responseText).error_description);
                            }
                        });
                    }

////////////////////////////

[{"districtName":"Haveri","districtCode":"11","talukName":"Haveri","talukCode":"1104","hobliName":"HAVERI","hobliCode":"110401","villageName":"Gowrapura.M.Adura","villageCode":"1104010013","message":"Data Available"}]

Friday 19 March 2021

Return a JSON string explicitly from Asp.net WEBAPI? or api return string as json in asp net web

 /////// 

There are a few alternatives. The simplest one is to have your method return a HttpResponseMessage, and create that response with a StringContent based on your string


Normal Code >>

        public HttpResponseMessage GetEmployees()
        {
            DataTable dt = new DataTable();
            var item = JsonConvert.SerializeObject(dt);            
            if (item != null && !string.IsNullOrEmpty(item))
            {               
                return Request.CreateResponse(HttpStatusCode.OK, item);
            }
            return Request.CreateResponse(HttpStatusCode.NotFound);
        }

Method >1.

public HttpResponseMessage Get() {

            string yourJson = GetJsonFromSomewhere();

            var response = this.Request.CreateResponse(HttpStatusCode.OK);

            response.Content = new StringContent(yourJson, Encoding.UTF8, "application/json");

            return response;

        }


Method >2.


public HttpResponseMessage Get()        {            

            string yourJson = GetJsonFromSomewhere();

            if (!string.IsNullOrEmpty(yourJson)) {

                var response = this.Request.CreateResponse(HttpStatusCode.OK);

                response.Content = new StringContent(yourJson, Encoding.UTF8, "application/json");

                return response;

            }

            throw new HttpResponseException(HttpStatusCode.NotFound);

        }



Excel Sort values in ascending order using function TEXTJOIN

 Excel ::  Text ::  1,3,5,2,9,5,11 Result :: 1,2,3,5,5,9,11 Formula ::     TEXTJOIN ( ",",1,SORT(MID(SUBSTITUTE( A1 ,","...