trx trx - 21 days ago 8
ASP.NET (C#) Question

Configure Web API to provide metadata

I am new to.NET /MVC/ Web API. I have created a .Net Web API which accepts the input parameters Queries the OracleDatabase and returns the result in JSON. Pretty much following the simple C# codes. My Controller looks like

public class DataController : ApiController
{
[HttpGet]
public HttpResponseMessage Getdetails(string ROOM, DateTime DOB_GT)
{
List<OracleParameter> prms = new List<OracleParameter>();
prms.Add(new OracleParameter("ROOM", OracleDbType.Varchar2, ROOM, ParameterDirection.Input));
prms.Add(new OracleParameter("DOB_GT", OracleDbType.Date, DOB_GT, ParameterDirection.Input));
string connStr = ConfigurationManager.ConnectionStrings["SDataBaseConnection"].ConnectionString;
using (OracleConnection dbconn = new OracleConnection(connStr))
{
DataSet userDataset = new DataSet();
var strQuery = "SELECT * from SAMPLE_RESULTS_VW where ROOM = :ROOM and DOB > :DOB_GT ";
var returnObject = new { data = new OracleDataTableJsonResponse(connStr, strQuery, prms.ToArray()) };
var response = Request.CreateResponse(HttpStatusCode.OK, returnObject, MediaTypeHeaderValue.Parse("application/json"));
ContentDispositionHeaderValue contentDisposition = null;
if (ContentDispositionHeaderValue.TryParse("inline; filename=TGSData.json", out contentDisposition))
{
response.Content.Headers.ContentDisposition = contentDisposition;
}
return response;
}


This works perfectly. Now the Client application is requesting for the metadata. So they can pull in the metadata from the service. I am not sure if we can configure the existing API to provide metadata. Or we will have to do smoething from the scratch

Answer

There are some projects available in the form of NuGet packages that can help you do this. So no need to write it from scratch.

You can add Swagger to provide metadata of your api. See https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger and https://github.com/domaindrivendev/Swashbuckle

Using the Asp.Net Web Api helppages (https://www.nuget.org/packages/Microsoft.AspNet.WebApi.HelpPage/) you can add WADL support using https://www.nuget.org/packages/leeksnet.AspNet.WebApi.Wadl/