Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web API with Swagger

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. And it works perfectly.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;
        }

But this Web API cannot be used as the Add service reference in the Visual Studio by the client application. So I tried to use Swagger, I installed them. I am getting the Swagger Page and able to test the API by giving query input and it does return result. enter image description here

I am not understanding what is the end point URL we will give the Client to use to get them metadata from the API. Or do I have do any further configuration to generate metadata. I am new to .NET and being stuck with this.

like image 290
trx Avatar asked Jan 17 '26 23:01

trx


2 Answers

I'm not sure what do you mean by client if you're using MVC.

Do you have two apps: a) front end .NET app AND b) backend .NET app that talks to the db?

Two .NET apps

Check out Get started with API Apps, ASP.NET, and Swagger in Azure App Service (it's not really relevant that it's for Azure).

This tutorial describes a setup where there are .NET apps and the one in the middle imports a swagger 'file' to consume the API of the back-end app which talks to the db. enter image description here

In my experience this setup is wasteful, but I don't know what your needs are.

One .NET app

If you have just one MVC app, then there really is no client.

This may be useful: Create an ASP.NET MVC app with auth and SQL DB and deploy to Azure App Service

like image 79
tymtam Avatar answered Jan 19 '26 15:01

tymtam


You give a client the URL to the swagger doc as documentation. The documentation gives you the URL to send the JSON too when you click the Try button.

like image 20
SledgeHammer Avatar answered Jan 19 '26 17:01

SledgeHammer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!