Vinay
Vinay

Reputation: 285

Web API get Version Info when base url is called

How can i send Version Info and few other values when WebApi's base URL is called.

Ex: http://199.169.818.513/api is the base url for my webapi.

When i enter the url i would like to receive a response with service details like, Web API Version, Name of the Site where its hosted etc

Upvotes: 0

Views: 2440

Answers (1)

Isaack Rasmussen
Isaack Rasmussen

Reputation: 457

You could create an API information class like this:

class ApiInformation{
  public string Version{get;set;
  public string Host{get;set;}
}

"IHttpActionResult Index()" method in your default controller, often called "HomeController.cs". This would then return the ApiInformation object which you will create an instance of first.

For Version, you could make use of:

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

from AssemblyInfo.cs. I usually change it to [assembly: AssemblyVersion("1.0.*")]

So the last two fields are auto generated for each compile. And then manually change the major and minor fields when necessary.

To get the version, you can call

System.Reflection.Assembly.GetExecutingAssembly().GetName().Version

To get domain name(for where its served from)

Request.RequestUri.Host

Or the IP address if that makes more sense in your scenario.

Once you filled out all the fields, you return it like this

return(Ok(apiInfo));

Ok() will return the details including HTTP-200 OK status.

Upvotes: 2

Related Questions