通过restclient调用asp.net webApi会引发异常"引用未声明的实体' nbsp'。第69行,第41位。"

时间:2017-06-14 03:42:11

标签: c# asp.net-web-api rest-client

我使用下面的代码

通过RestSharp从C#代码调用Asp.net WebApi
 RestClient Client = new RestClient();

                Client.BaseUrl = new Uri("http://localhost:27156");
                var request = new RestRequest("api/GetUserName", Method.POST);
                UserDto user = new UserDto { Ldap = "rahkuma"};
                request.AddHeader("Accept", "application/json");
                request.RequestFormat = DataFormat.Json;

                //request.AddJsonBody(user);
                request.AddBody(user);
                var response = Client.Execute<UserDto>(request);
  

服务抛出StatusCode:NotFound和ErrorException&#34;引用未声明的实体&#39; nbsp&#39;。第69行,第41位。&#34;   我能够从AngularJs中使用它,但是从C#(WebForm)使用restclient它会抛出异常。   任何有关这方面的帮助将不胜感激。   Web API控制器代码位于

之下
[HttpPost]
    public ResponseModel<UserDto> GetUserName(UserDto user)
    {
        string Role = "Read";


        ResponseModel<UserDto> response = null;
        try
        {
            var res = _accountLogic.GetUserName(new UserDto { Ldap = Utilities.Decrypt(user.Ldap) });

            if (res != null)
            {
                res.Ldap = user.Ldap;
                var roleDetail = _accountLogic.GetUserRoles(res.EmpLdap);
                if (roleDetail != null)
                {
                    res.Role = roleDetail.Role;
                    res.LockEdit = roleDetail.LockEdit;
                    res.ManagementAdjPlug = roleDetail.ManagementAdjPlug;
                }

                response = new ResponseModel<UserDto>();
                response.Data = res;
                response.ResponseStatus = Common.Enums.ResponseStatus.Sucess;
            }

        }
        catch (Exception ex)
        {

            response = new ResponseModel<UserDto>();
            response.ResponseStatus = Common.Enums.ResponseStatus.Error;
            response.ErrorMessage = ex.Message;
            throw ex;
        }


        return response;
    }

我还附上了 web Api exception detail image

下面的异常详情图片

2 个答案:

答案 0 :(得分:1)

原因是请求Url

中缺少Api控制器名称

当我从var request = new RestRequest("api/GetUserName", Method.POST);

进行更改时

至此var request = new RestRequest("api/Account/GetUserName", Method.POST);

它工作正常。完整代码位于

之下
 Client.BaseUrl = new Uri("http://localhost:27156");
            var request = new RestRequest("api/Account/GetUserName", Method.POST);
            UserDto user = new UserDto { Ldap = "rahkuma"};
            request.AddHeader("Accept", "application/json");
            request.RequestFormat = DataFormat.Json;

            //request.AddJsonBody(user);
            request.AddBody(user);
            var response = Client.Execute<UserDto>(request);

这是我身边的一个愚蠢的错误,感谢所有支持和帮助我的人

答案 1 :(得分:0)

您可以尝试对您的请求进行这些修改,看看是否有帮助:

RestClient Client = new RestClient("http://localhost:27156");
var request = new RestRequest("api/GetUserName", Method.POST);
UserDto user = new UserDto { Ldap = "rahkuma"};
request.Method = Method.POST;
request.AddHeader("Accept", "application/json");
request.RequestFormat = DataFormat.Json;
request.Method = Method.POST;

//request.AddJsonBody(user);
//request.AddBody(user);
request.AddParameter("application/json", user, ParameterType.RequestBody);
var response = Client.Execute<UserDto>(request);

编辑:

//request.AddJsonBody(user);
request.AddBody(user);    //same as your original
//request.AddParameter("application/json", user, ParameterType.RequestBody);
var response = Client.Execute(request); //modified

然后设置一个断点,或显示响应,看看究竟返回了什么。