【问题标题】:Querying Oracle DB using query parameters Web API使用查询参数 Web API 查询 Oracle DB
【发布时间】:2016-11-16 05:23:07
【问题描述】:

我们目前正在 Oracle DB 中查看。我们需要创建一个接受输入参数并在 Oracle DB 中查询视图并以 JSON 格式返回响应的 Web API。我是 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 }
        );        }}}

我不确定为什么会收到错误消息。我已更改任何内容或未重命名。任何人都可以帮我解决这个问题

【问题讨论】:

  • 您的错误消息显示您尝试了/api/TGSData 而不是/api/TGSSampData - 这可能是问题吗?
  • 显示 webapiconfig
  • 在您的路径 /api/TGSData 上找不到控制器,这就是错误告诉您的内容。此外,您在构建查询时出错。您的 id 应该在 '' 中,但最好使用 OracleCommandParameters !
  • @Beno 我很抱歉。我尝试更改应用程序并错过更新屏幕截图。我已经更新了问题
  • @Nkosi 我已经用 WebApiConfig.cs 更新了我的问题

标签: c# asp.net asp.net-mvc asp.net-web-api asp.net-web-api-routing


【解决方案1】:

Parameter Binding in ASP.NET Web API

使用 [FromUri]

要强制 Web API 从 URI 中读取复杂类型,请添加 [FromUri] 属性到参数。

删除 [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) { ... }
}

【讨论】:

  • NKosi 我进行了更改,但仍然遇到相同的错误。我试着给api/TGSSampData?id=379api/TGSSampData/379
  • 控制器中是否还有其他未包含在示例中的操作?
  • 不,这是唯一的,我还有其他处理 JSON 的类
  • 我们如何验证这一点。当我启动调试器http://localhost:24649/ 时,主页会出现。
  • 尝试通过浏览器中的默认路由或使用 postman 或 fiddler 调用 api 控制器。
猜你喜欢
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 2018-03-27
  • 1970-01-01
  • 2010-11-30
  • 2011-03-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多