Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# HttpClient.PostAsJsonAsync不工作或在调试模式下出现任何错误_C#_Asp.net Mvc_Asp.net Web Api - Fatal编程技术网

C# HttpClient.PostAsJsonAsync不工作或在调试模式下出现任何错误

C# HttpClient.PostAsJsonAsync不工作或在调试模式下出现任何错误,c#,asp.net-mvc,asp.net-web-api,C#,Asp.net Mvc,Asp.net Web Api,我在MVC中使用Web API时遇到了一些问题,不确定是什么原因造成的,但在调试模式下它不会抛出任何异常或错误,请有人帮助解决这个问题 代码如下: MVC控制器调用: PortalLogonCheckParams credParams = new PortalLogonCheckParams() {SecurityLogonLogonId = model.UserName, SecurityLogonPassword = model.Password}; SecurityLogon secur

我在MVC中使用Web API时遇到了一些问题,不确定是什么原因造成的,但在调试模式下它不会抛出任何异常或错误,请有人帮助解决这个问题

代码如下:

MVC控制器调用:

PortalLogonCheckParams credParams = new PortalLogonCheckParams() {SecurityLogonLogonId = model.UserName, SecurityLogonPassword = model.Password};

SecurityLogon secureLogon = new SecurityLogon();

var result = secureLogon.checkCredentials(credParams);
public async Task <IEnumerable<PortalLogon>> checkCredentials(PortalLogonCheckParams credParams)
{
    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri("http://localhost:50793/");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        // Check Credentials

        //Following call fails

        HttpResponseMessage response = await client.PostAsJsonAsync("api/chkPortalLogin", credParams);  


        if (response.IsSuccessStatusCode)
        {
            IEnumerable<PortalLogon> logonList = await response.Content.ReadAsAsync<IEnumerable<PortalLogon>>();
            return logonList;
        }
        else return null;
    }

}
[HttpPost]
public IHttpActionResult chkPortalLogin([FromBody] PortalLogonCheckParams LogonParams)
{

    List<Mod_chkPortalSecurityLogon> securityLogon = null;

    String strDBName = "";

    //Set the database identifier        
    strDBName = "Mod";

    //Retrieve the Logon object
    using (DataConnection connection = new DataConnection(strDBName))
    {
        //Retrieve the list object
        securityLogon = new Persistant_Mod_chkPortalSecurityLogon().findBy_Search(connection.Connection, LogonParams.SecurityLogonLogonId, LogonParams.SecurityLogonPassword);
    }

    AutoMapper.Mapper.CreateMap<Mod_chkPortalSecurityLogon, PortalLogon>();

    IEnumerable<PortalLogon> securityLogonNew = AutoMapper.Mapper.Map<IEnumerable<Mod_chkPortalSecurityLogon>, IEnumerable<PortalLogon>>(securityLogon);


    return Ok(securityLogonNew);

}
数据访问对象方法:

PortalLogonCheckParams credParams = new PortalLogonCheckParams() {SecurityLogonLogonId = model.UserName, SecurityLogonPassword = model.Password};

SecurityLogon secureLogon = new SecurityLogon();

var result = secureLogon.checkCredentials(credParams);
public async Task <IEnumerable<PortalLogon>> checkCredentials(PortalLogonCheckParams credParams)
{
    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri("http://localhost:50793/");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        // Check Credentials

        //Following call fails

        HttpResponseMessage response = await client.PostAsJsonAsync("api/chkPortalLogin", credParams);  


        if (response.IsSuccessStatusCode)
        {
            IEnumerable<PortalLogon> logonList = await response.Content.ReadAsAsync<IEnumerable<PortalLogon>>();
            return logonList;
        }
        else return null;
    }

}
[HttpPost]
public IHttpActionResult chkPortalLogin([FromBody] PortalLogonCheckParams LogonParams)
{

    List<Mod_chkPortalSecurityLogon> securityLogon = null;

    String strDBName = "";

    //Set the database identifier        
    strDBName = "Mod";

    //Retrieve the Logon object
    using (DataConnection connection = new DataConnection(strDBName))
    {
        //Retrieve the list object
        securityLogon = new Persistant_Mod_chkPortalSecurityLogon().findBy_Search(connection.Connection, LogonParams.SecurityLogonLogonId, LogonParams.SecurityLogonPassword);
    }

    AutoMapper.Mapper.CreateMap<Mod_chkPortalSecurityLogon, PortalLogon>();

    IEnumerable<PortalLogon> securityLogonNew = AutoMapper.Mapper.Map<IEnumerable<Mod_chkPortalSecurityLogon>, IEnumerable<PortalLogon>>(securityLogon);


    return Ok(securityLogonNew);

}
公共异步任务检查凭据(PortalLogonCheckParams credParams)
{
使用(var client=new HttpClient())
{
client.BaseAddress=新Uri(“http://localhost:50793/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(新的MediaTypeWithQualityHeaderValue(“应用程序/json”);
//检查凭证
//以下呼叫失败
HttpResponseMessage response=wait client.postsJSONASync(“api/chkPortalLogin”,credParams);
if(响应。IsSuccessStatusCode)
{
IEnumerable logonList=wait response.Content.ReadAsAsync();
返回登录列表;
}
否则返回null;
}
}
Web API:

PortalLogonCheckParams credParams = new PortalLogonCheckParams() {SecurityLogonLogonId = model.UserName, SecurityLogonPassword = model.Password};

SecurityLogon secureLogon = new SecurityLogon();

var result = secureLogon.checkCredentials(credParams);
public async Task <IEnumerable<PortalLogon>> checkCredentials(PortalLogonCheckParams credParams)
{
    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri("http://localhost:50793/");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        // Check Credentials

        //Following call fails

        HttpResponseMessage response = await client.PostAsJsonAsync("api/chkPortalLogin", credParams);  


        if (response.IsSuccessStatusCode)
        {
            IEnumerable<PortalLogon> logonList = await response.Content.ReadAsAsync<IEnumerable<PortalLogon>>();
            return logonList;
        }
        else return null;
    }

}
[HttpPost]
public IHttpActionResult chkPortalLogin([FromBody] PortalLogonCheckParams LogonParams)
{

    List<Mod_chkPortalSecurityLogon> securityLogon = null;

    String strDBName = "";

    //Set the database identifier        
    strDBName = "Mod";

    //Retrieve the Logon object
    using (DataConnection connection = new DataConnection(strDBName))
    {
        //Retrieve the list object
        securityLogon = new Persistant_Mod_chkPortalSecurityLogon().findBy_Search(connection.Connection, LogonParams.SecurityLogonLogonId, LogonParams.SecurityLogonPassword);
    }

    AutoMapper.Mapper.CreateMap<Mod_chkPortalSecurityLogon, PortalLogon>();

    IEnumerable<PortalLogon> securityLogonNew = AutoMapper.Mapper.Map<IEnumerable<Mod_chkPortalSecurityLogon>, IEnumerable<PortalLogon>>(securityLogon);


    return Ok(securityLogonNew);

}
[HttpPost]
public IHttpActionResult chkPortalLogin([FromBody]PortalLogonCheckParams LogonParams)
{
List securityLogon=null;
字符串strDBName=“”;
//设置数据库标识符
strDBName=“Mod”;
//检索登录对象
使用(DataConnection=newdataconnection(strDBName))
{
//检索列表对象
securityLogon=new Persistant_Mod_chkPortalSecurityLogon()。通过搜索查找(connection.connection,LogonParams.SecurityLogonLogonId,LogonParams.SecurityLogonPassword);
}
AutoMapper.Mapper.CreateMap();
IEnumerable securityLogonNew=AutoMapper.Mapper.Map(securityLogon);
返回Ok(securityLogonNew);
}

您需要从参数中删除
[FromBody]
属性

要强制Web API从请求正文读取简单类型,请添加 参数的[FromBody]属性:

public HttpResponseMessage Post([FromBody] string name) { ... }
在本例中,Web API将使用媒体类型格式化程序来读取 请求正文中名称的值。下面是一个示例客户端 请求

POST http://localhost:5076/api/values HTTP/1.1
User-Agent: Fiddler
Host: localhost:5076
Content-Type: application/json
Content-Length: 7

"Alice"
当参数具有[FromBody]时,Web API使用内容类型标头 选择格式化程序。在本例中,内容类型为 “application/json”,请求主体是一个原始json字符串(不是 JSON对象)

最多允许从消息体读取一个参数


您是否尝试过通过fiddler或SOAP UI进行调用,如果是的话,响应代码是什么?您所说的“不工作”是什么意思?哪里出了问题?您希望发生什么?从参数中删除
[FromBody]
属性。