使用查询参数 Web API 查询 Oracle DB
Querying Oracle DB using query parameters Web API
我们目前在 Oracle 数据库中有一个视图。我们需要创建一个 Web API 接受输入参数并查询 Oracle DB 中的视图和 returns JSON 格式的响应。我是 ASP.NET 和 Web 服务的新手。下面是服务的代码
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;
}
}
我正在尝试调试,在 URL 我给了 http://localhost:6897/api/TGSSampData?id=379 但它抛出了错误,比如在此处输入图像描述
我没有用 RouteConfig.cs 或 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 }
); }}}
我不确定为什么会收到错误消息。我是否更改了任何内容 renamed.Can 任何人都请帮助我
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.
删除 [FromUri] 属性,您也可以使用 [HttpGet] 属性。
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) { ... }
}
我们目前在 Oracle 数据库中有一个视图。我们需要创建一个 Web API 接受输入参数并查询 Oracle DB 中的视图和 returns JSON 格式的响应。我是 ASP.NET 和 Web 服务的新手。下面是服务的代码
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;
}
}
我正在尝试调试,在 URL 我给了 http://localhost:6897/api/TGSSampData?id=379 但它抛出了错误,比如在此处输入图像描述
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 }
); }}}
我不确定为什么会收到错误消息。我是否更改了任何内容 renamed.Can 任何人都请帮助我
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.
删除 [FromUri] 属性,您也可以使用 [HttpGet] 属性。
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) { ... }
}