简体   繁体   English

同一应用程序上的Web API端点

[英]Web API endpoints on the same Application

I am having a working Web API application which has the controller called StudyDataController which accepts the input parameters, queries Database and returns result in JSON.That piece is working fine. 我有一个正在运行的Web API应用程序,该应用程序具有名为StudyDataController的控制器,该控制器可以接受输入参数,查询数据库并以JSON返回结果。 Calling http://localhost:3214/api/StudyDataController?param1=R01&param2=05-NOV-16 returns result. 调用http://localhost:3214/api/StudyDataController?param1=R01&param2=05-NOV-16返回结果。

public class StudyDataController : ApiController
{  [HttpGet]
   public HttpResponseMessage Getdetails(string param1, DateTime param2)
    {
            List<OracleParameter> p = new List<OracleParameter>();
            p.Add(new OracleParameter("param1", OracleDbType.Varchar2,param1, ParameterDirection.Input));
            p.Add(new OracleParameter("param2",OracleDbType.Date,param2, ParameterDirection.Input));
            string connStr = ConfigurationManager.ConnectionStrings["StudyDataConnection"].ConnectionString;
            using (OracleConnection dbconn = new OracleConnection(connStr))
            {
                DataSet userDataset = new DataSet();
                var strQuery = "SELECT * from Studydata_VW where Request_id = :param1 and RequestDate > :param2 ";
                var returnObject = new { data = new OracleDataTableJsonResponse(connStr, strQuery, prms.ToArray()) };
                var response = Request.CreateResponse(HttpStatusCode.OK, returnObject, MediaTypeHeaderValue.Parse("application/json"));
                ContentDispositionHeaderValue contentDisposition = null;
                if (ContentDispositionHeaderValue.TryParse("inline; filename=StudyData.json", out contentDisposition))
                {
                    response.Content.Headers.ContentDisposition = contentDisposition;
                }
                return response;
               }}}

But I now need to create a one more API has different input parameters and query is different and returns result in JSON. 但是我现在需要创建一个具有不同输入参数的API,并且查询也不同,并以JSON返回结果。 For this I am trying to create one Controller called StudyDatawithDateController on the same application. 为此,我试图在同一应用程序上创建一个名为StudyDatawithDateController控制器。

public class StudyDatawithDateController : ApiController
{  [HttpGet]
   public HttpResponseMessage Getdetails(DateTime param1,String param2)
    {
            List<OracleParameter> p = new List<OracleParameter>();
            p.Add(new OracleParameter("param1",OracleDbType.Date,param1, ParameterDirection.Input));
            p.Add(new OracleParameter("param2", OracleDbType.Varchar2,param2, ParameterDirection.Input));

            string connStr = ConfigurationManager.ConnectionStrings["StudyDataConnection"].ConnectionString;
            using (OracleConnection dbconn = new OracleConnection(connStr))
            {
                DataSet userDataset = new DataSet();
                var strQuery = "SELECT * from Studydata_VW where Submit_date> :param1 and Status = :param2 ";
                var returnObject = new { data = new OracleDataTableJsonResponse(connStr, strQuery, prms.ToArray()) };
                var response = Request.CreateResponse(HttpStatusCode.OK, returnObject, MediaTypeHeaderValue.Parse("application/json"));
                ContentDispositionHeaderValue contentDisposition = null;
                if (ContentDispositionHeaderValue.TryParse("inline; filename=StudyDatawithDate.json", out contentDisposition))
                {
                    response.Content.Headers.ContentDisposition = contentDisposition;
                }
                return response;
               }}}

If I try to call the endpoint like http://localhost:3214/api/StudyDatawithDateController?param1=01-NOV-16&param2=COMPLETE it says Not Found Error . 如果我尝试调用类似http://localhost:3214/api/StudyDatawithDateController?param1=01-NOV-16&param2=COMPLETE的端点,则会显示“ Not Found Error 在此处输入图片说明

The webConfig.cs is like below webConfig.cs如下所示

 public static class WebApiConfig
 {
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }

Can we have two controllers on the same WebAPI application. 我们可以在同一个WebAPI应用程序上有两个控制器吗? Most of the questions/Samples are discussing about having same name controllers. 大多数问题/示例都在讨论有关具有相同名称控制器的问题。 But here I am having different names and how can I deal with this. 但是在这里我有不同的名字,我该如何处理。

You can have many routes using get request going to one and the same Controller but different actions with different attribute routes. 使用get请求,您可以拥有许多路由,这些路由将到达同一控制器,但具有不同属性路由的不同操作。 Just add attribute routing on top of the method you want the route to process 只需在要处理路由的方法之上添加属性路由

[HttpGet] // http://localhost:3214/api/StudyDatawithDateController?param1=01-NOV-16&param2=COMPLETE [Route("api/StudyDatawithDateController/param1/{param1:string}/param2/{param2:string"})] [HttpGet] // http:// localhost:3214 / api / StudyDatawithDateController?param1 = 01-NOV-16&param2 = COMPLETE [Route(“ api / StudyDatawithDateController / param1 / {param1:string} / param2 / {param2:string”} )]

http://localhost:3214/api/StudyDataController?param1=R01&param2=05-NOV-16 [Route("api/StudyDataController/param1/{param1:string}/param2/{param2:string")] http:// localhost:3214 / api / StudyDataController?param1 = R01&param2 = 05-NOV-16 [Route(“ api / StudyDataController / param1 / {param1:string} / param2 / {param2:string”)]

The example above will still not work. 上面的示例仍然无法正常工作。 but if you change the url - specifically param2 and param1 - give them some descriptive names and in the endpoints you will have different parameters. 但是,如果您更改url(尤其是param2和param1),请为它们提供一些描述性名称,并且在端点中您将具有不同的参数。

param2 in the second example is Date and in the first string saying COMPLATE. 在第二个示例中,param2是Date,在第一个字符串中是COMPLATE。 - change that add the attribute routes and you will be fine. -更改添加属性路由,即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM