0

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. Calling http://localhost:3214/api/StudyDataController?param1=R01&param2=05-NOV-16 returns result.

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. For this I am trying to create one Controller called StudyDatawithDateController on the same application.

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. enter image description here

The webConfig.cs is like below

 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. 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.

8
  • 2
    You can have as many controller as you want. Did you forget to add the http method attribute? You need to post your code. Commented Nov 23, 2016 at 15:15
  • Probably yes. bou're probaby going to have to clarify your question. Maybe some code Commented Nov 23, 2016 at 15:16
  • @Marco I have edited the question. Can you please tell me what I am missing here Commented Nov 23, 2016 at 15:38
  • @YounElan I have edited the question. Can you please tell me what I am missing here Commented Nov 23, 2016 at 15:38
  • 2
    your url is StudyDatController missing an a? Commented Nov 23, 2016 at 15:39

1 Answer 1

1

You can have many routes using get request going to one and the same Controller but different actions with different attribute routes. 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"})]

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.

param2 in the second example is Date and in the first string saying COMPLATE. - change that add the attribute routes and you will be fine.

Sign up to request clarification or add additional context in comments.

9 Comments

I dont understand about iscomplate
it is just example parameter based on COMPLETE in [HttpGet] //localhost:3214/api/… Looking at it now I made a mistake defining the example routes will change it now
Not sure this entirely makes sense. You say "You can have many routes using get request going to one and the same Controller" but then give examples of URLs which go to two different controllers. Correct me if I'm wrong but with the examples as given I don't think you need the Route attributes.
@trx TBH I can't see why your code doesn't work as it currently exists. There are two different controllers, with different GET methods, the default route config is in use and the method names match the normal conventions in Web API 2, but the second one appears not to work. It doesn't seem to me like routing is the issue. I feel like something else is going on. It would be useful if you would share the exact HTTP status code which is returned when you attempt to make the request, and/or any exception which is generated by the API.
@ADyson I tried cecking in the fiddler and the error I am getting is {"Message":"No HTTP resource was found that matches the request URI 'localhost:3214/api/StudyDataWithDate?param1‌​=01-NOV-16&param2=CO‌​MPLETE'.","MessageDetail":"No type was found that matches the controller named 'StudyDataWithDate'."}
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.