1

I am creating a webservice using asp.net 4.0.

I have created a asmx file and creating a User.cs Class. It has 8 Properties. I have return a service with json format. If the userlogin is true i need to return all the properties of user.cs, if it's fail i need to return only 2 property.

How to achieve it.

User login is true. It will return all

{"message":"valid user","BranchId":1,"BranchName":"My branch Name","Id":1,"Name":"admin","Password":"admin","RoleId":1,"Status":1}

User login is failed i need to retun only message and Status. but it will return all like as foloows

{"message":"username or password is invalid","BranchId":0,"BranchName":null,"Id":0,"Name":null,"Password":null,"RoleId":0,"Status":0}

I have google it and get the following Link. How to use it based on my login status condition.

If i have used [ScriptIgnore] in my property it will ignore property both case. I need to ignore property when login failed.

My properties like this

// Properties
    public int BranchId
    {
        get { return _BranchId; }
        set { if (_BranchId != value) { _BranchId = value; } }
    }

    public string BranchName
    {
        get { return _BranchName; }
        set { _BranchName = value; }
    }

    private String _message;

    public String message
    {
        get { return _message; }
        set { _message = value; }
    }

My webservice

[WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public void appLogin(String userName, String passWord)
    {
        Admin_AppUserBLL objusr = Admin_AppUserBLL.GetAdmin_AppUserBLL(userName);
        string strResponse = "";
        if (objusr != null)
        {
            if (objusr.Password == passWord)
            {
                objusr.message = "valid username";
                strResponse = new JavaScriptSerializer().Serialize(objusr);
            }
        }
        else
        {
            objusr = new Admin_AppUserBLL();
            objusr.message = "username or password is invalid";
            strResponse = new JavaScriptSerializer().Serialize(objusr);
        }
        Context.Response.Clear();
        Context.Response.ContentType = "application/json";
        Context.Response.AddHeader("content-length", strResponse.Length.ToString());
        Context.Response.Flush();
        Context.Response.Write(strResponse);
    }
Hisanth
  • 65
  • 2
  • 14
  • Please check [here](https://stackoverflow.com/questions/1387755/can-javascriptserializer-exclude-properties-with-null-default-values) - possible duplicate – Shah Oct 27 '17 at 09:39

2 Answers2

1

Add following attribute on your property and also make it nullable by using "?"

[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "BranchId")]
        public int? BranchId{ get; set; }

It will ignore if value will be null and also json does not contain these peoperties.

Shyam Sa
  • 331
  • 4
  • 8
  • It shows following error, The type or namespace name 'JsonProperty' could not be found (are you missing a using directive or an assembly reference? – Hisanth Oct 27 '17 at 08:31
  • Add reference of NewtonSoft in your application. If it is solved your problem please mark solution. – Shyam Sa Oct 27 '17 at 09:32
  • @Hisanth is using JavaScriptSerializer not NewtonSoft – Shah Oct 27 '17 at 09:41
  • It is a part of Json.net Refer the following link https://www.newtonsoft.com/json/help/html/JsonPropertyName.htm – Shyam Sa Oct 27 '17 at 09:56
0

Add reference in Newtonsoft

using Newtonsoft.Json;

while serialize the object

string strResponse = JsonConvert.SerializeObject(objusr, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });

it will avoid null value property

Hisanth
  • 65
  • 2
  • 14