从razor模板调用外部API

问题描述

我正在尝试将MS Dynamics API JSON响应用作2sxc剃刀模板中的数据源。出于安全原因,我想将查询生成为服务器端发布请求。我的代码必须存在于razor模板中,因为我无法通过FTP访问上传自定义代码或库。

我一直尝试在自定义应用中使用的标准异步请求。

  private static readonly HttpClient client = new HttpClient();


        protected void Page_Load(object sender,EventArgs e)
        {
            GetData();
        }


        public async void GetData() {

            var requestparams = new Dictionary<string,string>
            {
                { "key","123456789" },{ "Operation","GetEntityTypes" }
            };

            var content = new FormUrlEncodedContent(requestparams);

            var response = await client.PostAsync("https://api.someserver.com/",content);

            var responseString = await response.Content.ReadAsStringAsync();

            Response.Write(responseString);

        }

当我在2sxc剃须刀页面页面内尝试时,收到错误消息


Error: System.InvalidOperationException: An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. 

我知道可能有一种更性感的方式来做到这一点,但我正在空白。

谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)