trx trx - 1 year ago 149
ASP.NET (C#) Question

Querying Oracle DB using query parameters Web API

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)

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

Answer Source

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 
    public HttpResponseMessage Get(string id) { ... }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download