Vincent Dagpin
Vincent Dagpin

Reputation: 3611

Get the Project URL in WebAPI

How to get the project URL in C# inside the server in my case which is http://localhost:4333/ because we have this

Image _img = Image.GetInstance("http://localhost:4333/logo.PNG");

in the server to print the PDF with logo in it then send it to client as byte[]

is there something like

string _projectUrl = Environment.GetProjectUrl;
Image _img = Image.GetInstance(_projectUrl + "/guavatel.PNG");

Upvotes: 0

Views: 3186

Answers (1)

Jämes
Jämes

Reputation: 7235

In WebAPI and more specifically in a context of an ApiController, the Request property of type HttpRequestMessage is different than in MVC applications. Therefore, HttpRequestMessage doesn't define a property called Url.

For that purpose, you have to use the property called RequestUri.

You can get your project base url with this extension method:

public static class ApiControllerExtensions
{
    public static string GetBaseUrl(this ApiController controller)
    {
        return string.Concat(controller.Request.RequestUri.Scheme, "://", controller.Request.RequestUri.Authority);
    }
}

Upvotes: 2

Related Questions