简体   繁体   English

如何将 ihttpactionresult 返回到字符串

[英]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.我是 C# 的新手,我试图从 ihttpactionresult 返回到变量中获取一个值。

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.在 UserRole 中,我从一个等于 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.我试图从 GetUserRole() function 中获取值 SMPAG 到 userRole 并进行一些操作,但是除了传递的字符串之外,我得到了一些其他值如何在此变量中获取 SMPAG 的字符串值。 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?您通常需要将用户角色作为string值,对吗? But the GetUserRole web method returns an IHttpActionResult .但是GetUserRole web 方法返回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:引入第三种方法GetUserRoleImplementation (或任何对您有意义的名称)将有助于解决此问题:

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.您可以看到我们已将现有GetUserRole方法的大部分功能移至此处。 That means that GetUserRole now simply looks like this:这意味着GetUserRole现在看起来像这样:

[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:由于我们不再需要弄清楚如何从IHttpActionResult中提取字符串值, GetTableValue现在看起来像这样:

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

        var UserID = UserAdId;

        var UserRole = GetUserRoleImplementation();

        return Ok(UserRole );
    }
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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