Quantcast
Channel: WCF, ASMX and other Web Services
Viewing all articles
Browse latest Browse all 555

How to avoid null value property class property from Json Serialization

$
0
0

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 return only message and Status. but it will return all like as follows

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

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 service.asmx

[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);
    }


Viewing all articles
Browse latest Browse all 555

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>