简体   繁体   中英

how to convert ihttpactionresult return to string

I am new to C# and am trying to get a value from the return of ihttpactionresult to a variable.

However I am having difficulty in this. This is my method:

[HttpPost]
        [Route("GetUserRole")]
        public IHttpActionResult GetUserRole()
        {
            ManageUserData ObjManageUserData = new ManageUserData();
            string UserRole = string.Empty;
            var dt = ObjManageUserData.GetUserRole(UserPersonalNumber);
            if (dt.Rows.Count > 0)
                UserRole = dt.Rows[0][0].ToString();
            else
                UserRole = "User";
            return Ok(UserRole);
        }

In UserRole I am getting value from a table which is equal to SMPAG. I am trying to get this value to a variable in another string however I am having difficulties.

The other method is:

[HttpPost]
        [Route("GetTableValue")]
        public IHttpActionResult GetTableValue()
        {
            try
            {
                List<UserModel> userModels = new List<UserModel>();

                var UserID = UserAdId;

                var UserRole = GetUserRole().ToString();

                return Ok(UserRole );
                }

I am trying to get the value SMPAG from GetUserRole() function into userRole and do some manipulation however I am getting some other value other than the string passed how do I get the string value of SMPAG in this variable. Could you please help?

I think you're going about this in the wrong way. You have a common need for the user's role as a string value, right? But the GetUserRole web method returns an IHttpActionResult . So you're effectively generating a string value, wrapping it up, and then you want to unwrap it again to use it.

The introduction of a third method, GetUserRoleImplementation (or what ever name makes sense to you), will help with this:

private string GetUserRoleImplementation()
{
    ManageUserData ObjManageUserData = new ManageUserData();
    string UserRole = string.Empty;
    var dt = ObjManageUserData.GetUserRole(UserPersonalNumber);
    if (dt.Rows.Count > 0)
        UserRole = dt.Rows[0][0].ToString();
    else
        UserRole = "User";
}

You can see that we've moved the bulk of the functionality of the existing GetUserRole method here. That means that GetUserRole now simply looks like this:

[HttpPost]
[Route("GetUserRole")]
public IHttpActionResult GetUserRole()
{
    string userRole = GetUserRoleImplementation();
    return Ok(UserRole);
    // or just: return Ok(GetUserRoleImplementation());
}

And because we don't have to figure out how to extract a string value from an IHttpActionResult anymore, GetTableValue now looks like this:

[HttpPost]
[Route("GetTableValue")]
public IHttpActionResult GetTableValue()
{
    try
    {
        List<UserModel> userModels = new List<UserModel>();

        var UserID = UserAdId;

        var UserRole = GetUserRoleImplementation();

        return Ok(UserRole );
    }
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM