0

I am trying to add one more controller to our existing Web API. The controllers are like

public class TDataController : ApiController
{        
   [HttpGet]
   public HttpResponseMessage Getdetails(string ROOM, DateTime DOB_GT, DateTime DOB_LT, string STATUS_TYPE)
   { 
     // Code for the controller
   }
}

and this is the controller I am trying to add in the same Application

public class TDataSubDateController : ApiController
{
   [HttpGet]
   public HttpResponseMessage Getdetails(string ROOM, string STATUS_TYPE, DateTime? SUBDATE_GT = null, DateTime? SUBDATE_LT = null)
   {
     //Code for the controller
   }
}

When I am trying to call the second controller like

http://localhost:33823/api/TDataSubDate?ROOM=xxx&STATUS_TYPE=xxx&SUBDATE_GT=xxxx&SUBDATE_LT=xxxx

But it throws the HTTP 404 Page Not Founderror. Do I have to create a different route in the WebConfig.cs. The RouteConfig.cs currently looks like

     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 }
        );
    }
}
9
  • That route config is for MVC, not for Web API. Commented Nov 22, 2016 at 18:03
  • How do I handle with two Controller in the same API. To test I used the same code and create seperate Web API and it works perfectly and returns result Commented Nov 22, 2016 at 18:06
  • You need to find where your Web API routing is configured. Probably WebApiConfig.cs if you used default template. Routing is all explained here. Commented Nov 22, 2016 at 18:08
  • I have edited my question with the WebConfig.cs. Do I have create a new route here? Commented Nov 22, 2016 at 18:11
  • 1
    Try using route attributes: asp.net/web-api/overview/web-api-routing-and-actions/… Commented Nov 22, 2016 at 18:46

1 Answer 1

4

You can use attribute routing if you are using web api 2. For more details please visit https://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

Here is an example with single controller and multiple actions

[RoutePrefix("api/tdata")]
public class TDataController : ApiController
{        
    [HttpGet]
    [Route("{ROOM}/preview")]
    public IHttpActionResult Getdetails(string ROOM, [FromUri]DateTime DOB_GT, [FromUri]DateTime DOB_LT, [FromUri]string STATUS_TYPE)
    { 
        return Ok(string.Format("Room {0} Preview", ROOM));
    }

    [HttpGet]
    [Route("{ROOM}/details")]
    public IHttpActionResult Getdetails(string ROOM, [FromUri]string STATUS_TYPE, [FromUri]DateTime? SUBDATE_GT = null, [FromUri]DateTime? SUBDATE_LT = null)
    {
        return Ok(string.Format("Room {0} Details", ROOM));
    }
}

OR into a separate controller

[RoutePrefix("api/tdatasubdate")]
public class TDataSubDateController : ApiController
{
    [HttpGet]
    public IHttpActionResult Getdetails([FromUri]string ROOM, [FromUri]string STATUS_TYPE, [FromUri]DateTime? SUBDATE_GT = null, [FromUri]DateTime? SUBDATE_LT = null)
    {
        return Ok(string.Format("Room {0} Details", ROOM));
    }
}

And here is how webapiconfig.cs looks like

public static class WebApiConfig
{
    /// <summary>
    /// configure global routes
    /// </summary>
    /// <param name="config"></param>
    public static void Register(HttpConfiguration config)
    {
        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Dont we have to change anything in WebConfig.cs ? I tried to add [Route("api/tdatasubdate")] And tried to call the API as http://localhost:33823/api/tdatasubdate?ROOM=xxx&STATUS_TYPE=xxx&SUBDATE_GT=xxxx&SUBDATE_LT=xxxx It is still not working
Do you have the following statement as part of your application startup? GlobalConfiguration.Configure(WebApiConfig.Register); usually it goes in the global.asax.cs
Yes I have the statement in Global.asax.cs. And I am able browse through the other controller TData. It is just the issue with the TDataSubDate controller
What is the url for the controller that works? Can you reproduce the error in another test project?

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.