How to get the Relative Server Path in ASP.NET Web API ?

In ASP.NET MVC , we can use the Server.MapPath or Request.MapPath to get the relative path of the server for a file.

For example ,

var fileName = Request.MapPath("~/Content/files/abundantcode.jpg");

The fileName would be the absolute path of the file.

How to get the Relative Server Path in ASP.NET Web API ?

You can use the HostingEnvironment.MapPath defined in System.Web.Hosting to get the relative path of the file as shown below.

var filePath = System.Web.Hosting.HostingEnvironment.MapPath("~/Content/files/abundantcode.jpg");
%d