1

We are currently having a view in the Oracle DB. We need to create a Web API that accepts the input parameters and queries the view in the Oracle DB and returns the response in the JSON format. I am new to ASP.NET and the web services. Below is the code for the service

namespace TGSSample.Controllers
{
public class TGSSampDataController : ApiController
{
    public HttpResponseMessage Getdetails([FromUri] string id)
    {

        List<OracleParameter> prms = new List<OracleParameter>();
        List<string> selectionStrings = new List<string>();
        string connStr = ConfigurationManager.ConnectionStrings["TGSDataConnection"].ConnectionString;
        using (OracleConnection dbconn = new OracleConnection(connStr))
        {
            DataSet userDataset = new DataSet();
            var strQuery = "SELECT * from LIMS_SAMPLE_RESULTS_VW where JRS_NO =" + id;

            var returnObject = new { data = new OracleDataTableJsonResponses(connStr, strQuery, prms.ToArray()) };
            var response = Request.CreateResponse(HttpStatusCode.OK, returnObject, MediaTypeHeaderValue.Parse("application/json"));
            ContentDispositionHeaderValue contentDisposition = null;
            if (ContentDispositionHeaderValue.TryParse("inline; filename=ProvantisStudyData.json", out contentDisposition))
            {
                response.Content.Headers.ContentDisposition = contentDisposition;
            }
            return response;
        }
    }

I am trying to debug and in the URL I gave like http://localhost:6897/api/TGSSampData?id=379 but it throws error like enter image description here enter image description here I havent changed anything with the RouteConfig.cs or WebApiConfig.cs.

namespace TGSSample
{
 public static class WebApiConfig
 {
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();

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

I am not sure why I am getting the error. I have changed anything or not renamed.Can anyone please help me with this enter image description here

9
  • Your error message says you tried /api/TGSData not /api/TGSSampData - could that be the issue? Commented Nov 16, 2016 at 5:29
  • show webapiconfig Commented Nov 16, 2016 at 5:30
  • No controller is found on your path /api/TGSData, this is what the error saying to you. Also you have an error in building of your query. Your id should be in '', but better use OracleCommandParameters ! Commented Nov 16, 2016 at 5:31
  • @Beno My apologies. I tried changing the application and missed updating the screenshot. I have updated the question Commented Nov 16, 2016 at 5:35
  • @Nkosi I have updated my question with the WebApiConfig.cs Commented Nov 16, 2016 at 5:37

1 Answer 1

1

Parameter Binding in ASP.NET Web API

Using [FromUri]

To force Web API to read a complex type from the URI, add the [FromUri] attribute to the parameter.

Remove the [FromUri] attribute and you can use the [HttpGet] attribute as well.

public class TGSSampDataController : ApiController {
    //according to convention-based route mapping in webapiconfig
    //api/{controller}/{id} should map the following to this action
    //GET api/TGSSampData?id=379 
    //GET api/TGSSampData/379 
    [HttpGet]
    public HttpResponseMessage Get(string id) { ... }
}
Sign up to request clarification or add additional context in comments.

9 Comments

NKosi I made the change but still getting the same error. I tried giving as api/TGSSampData?id=379 and also api/TGSSampData/379
Are there any other actions in the controller that was not included in the example?
No this is the only one, I have other classes for dealing with the JSON
How do we verify that. When I start the debugger http://localhost:24649/ the Home page comes up.
try calling an api controller via the default route in the browser or using postman or fiddler.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.