Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.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# 从浏览器调用web API不使用异步操作_C#_Asynchronous_Asp.net Web Api - Fatal编程技术网

C# 从浏览器调用web API不使用异步操作

C# 从浏览器调用web API不使用异步操作,c#,asynchronous,asp.net-web-api,C#,Asynchronous,Asp.net Web Api,我已经创建了一个具有以下异步操作的web API [ActionName("getFedCust")] private async Task<HttpResponseMessage> getFedCust() { using (var client = new HttpClient()) { client.BaseAddress = new Uri("http://fw.dev.test.com/");

我已经创建了一个具有以下异步操作的web API

[ActionName("getFedCust")]
    private async Task<HttpResponseMessage> getFedCust()
    {
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri("http://fw.dev.test.com/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage response = await client.GetAsync("/api/fed");                
            return response;               

        }
    }
顺便说一句,如果我创建一个非异步操作,它就可以工作

    public HttpResponseMessage getFedCust()
{
    FedCust[] fedcust = new FedCust[]
    {
        new FedCust {name="Fabrikam"
            ,domains="fabrikam.local"
            ,sts="http://fabrikam.test.com/adfs/services/trust"
        },
        new FedCust {name="Contoso"
            ,domains="contoso.local"
            ,sts="http://contoso.test.com/adfs/services/trust"
        }
    };
    return this.Request.CreateResponse(HttpStatusCode.OK, fedcust, "application/json");
}

欢迎您提供任何帮助,因为我对这一部分非常熟悉

我认为您错误地将异步操作定义为“private”

private异步任务getFedCust()
应该是:

public async Task<HttpResponseMessage> getFedCust()
public异步任务getFedCust()
定义“不工作”。
private async Task<HttpResponseMessage> getFedCust()
public async Task<HttpResponseMessage> getFedCust()