Click here to Skip to main content
15,919,749 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing web api's in json using asp.net c#. I want http status code with output parameters in response of api's .how I can I achieve this.

What I have tried:

C#
[WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public void GetUserLogin(string Contract, string username
        {
            Authentication obj = new Authentication();
            String jSonString = "";
            System.Web.Script.Serialization.JavaScriptSerializer serializer =
             new System.Web.Script.Serialization.JavaScriptSerializer();
            SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["conststring"]);
            SqlCommand command = new SqlCommand();
            try
            {
                    DataSet ds = new DataSet();
                    ds = obj.CheckContract(Contract);

                    if (ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    {
                        DataSet dscustomer = new DataSet();
                        dscustomer = obj.CheckCustomer(ds.Tables[0].Rows[0]["Customer"].ToString(), username

                        if (dscustomer.Tables[0] != null && dscustomer.Tables[0].Rows.Count > 0)
                        {
                         
                        string success = "";
                        success = "{\"Customer\": \" " + ds.Tables[0].Rows[0]["Customer"].ToString() + " \"}";
                        this.Context.Response.ContentType = "application/json; charset=utf-8";
                        this.Context.Response.Write(success);
                        
                        }
                        else
                        {
                            string success = "";
                            success = "{\"status\": \"Invalid Login Details.\"}";
                            this.Context.Response.ContentType = "application/json; charset=utf-8";
                            this.Context.Response.Write(success);
                        }

                    }
                    else
                    {
                        string success = "";
                        success = "{\"status\": \"Invalid Login Details.\"}";
                        this.Context.Response.ContentType = "application/json; charset=utf-8";
                        this.Context.Response.Write(success);
                    }
                
               
            }

            catch (Exception er)
            {
                string success = "";
                success = "{\"status\": \"" + er.Message + "\"}";
                this.Context.Response.ContentType = "application/json; charset=utf-8";
                this.Context.Response.Write(success);

            }
            finally
            {
                command.Dispose();
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                    conn.Dispose();
                }
            }
        }
Posted
Updated 27-Sep-16 23:37pm
v2

1 solution

I googled "asp.net webmethod set status code" for you and here are some results

asp.net - Can I Set the HTTP Response Code & Throw an Exception on an ASMX JSON Service? - Stack Overflow[^]

c# - Asp.Net web service: I would like to return error 403 forbidden - Stack Overflow[^]

You could easily have done this yourself, please do basic research before asking a question.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900