如何从Visual C#

时间:2017-12-08 09:45:37

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

我在visual c#中使用bot框架创建了一个bot应用程序,我在Azure中发布了它。我的数据库也在Azure中。我通过bot给出CompanyId,TenantId,SiteId和Bandid,然后从数据库回复为CompanyName和Address。它用TestData.cs类编写。 我还附加了TestAPIController,我希望将数据库中的数据导入到bot应用程序中。如何编写我的RootDialog.cs?

public class TestAPIController : ApiController
{

    [HttpGet]
    public IHttpActionResult Get(string TenantId, string CompanyId, string SiteId, string BandId)
    {
        TestData TestData = new TestData();
        var TestBusiness = TestData.Select(TenantId, CompanyId, SiteId, BandId);

        return Ok(TestBusiness);
    }

}    

public class TestData :DBConnection
{

    public List<TestBusiness> Select(string TenantId, string CompanyId, string SiteId, string BandId)
    {
        List<TestBusiness> list = new List<TestBusiness>();
        this.StoredProcedure = "RESQBOTGetCompanywiseData";
        AddParameter("TenantId", TenantId);
        AddParameter("CompanyId", CompanyId);
        AddParameter("SiteId", SiteId);
        AddParameter("BandId", BandId);
        DataTable dt = this.ExecuteSelect();
        foreach (DataRow dr in dt.Select())
        {
            TestBusiness item = new TestBusiness();
            item.CompanyName = dr["CompanyName"].ToString();
            item.Address = dr["Address"].ToString();
            list.Add(item);
        }
        return list;


    }
}

2 个答案:

答案 0 :(得分:0)

你必须使用允许你调用webapi的HttpClient并获取下面的数据是示例代码

using(var client = newHttpClient())  
{  
    client.BaseAddress = newUri("http://localhost:55587/");  
    client.DefaultRequestHeaders.Accept.Clear();  
    client.DefaultRequestHeaders.Accept.Add
              (newMediaTypeWithQualityHeaderValue("application/json"));  
    //GET Method  
    HttpResponseMessage response = 
              await client.GetAsync("GetAllRows/1/1/1/1");  
    if (response.IsSuccessStatusCode)  
    {  
    }  
}  

您还需要在web api中设置路由,如下所示,并在调用此方法时传递所有参数

[Route("GetAllRows/{TenantId}/{CompanyId}/{SiteId}/{BandId}")]
 public IHttpActionResult Get(string TenantId, string CompanyId,
                   string SiteId, string BandId)
{
}

还为基于路由属性的路由配置webapi

   public static void Register(HttpConfiguration config)
        {
            config.MapHttpAttributeRoutes();

       }

请遵循以下文章:https://dzone.com/articles/web-api-with-httpclient-or-consume-web-api-from-co

答案 1 :(得分:0)

在RootDialog.cs中,我创建了TestAPIController实例的对象。然后调用那个并将输入作为参数传递。我显示数据库的第一个数据中的数据。

TestAPIController Test = new TestAPIController();
returnData = Test.Get(TenantId, CompanyId, SiteId, BandId);

string companyName = returnData[0].CompanyName.ToString();
string address= returnData[0].Address.ToString();

await context.PostAsync("Company Name = "+ companyName + " and It's address is "+ address);