Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get base URL in Web API controller?

I know that I can use Url.Link() to get URL of a specific route, but how can I get Web API base URL in Web API controller?

like image 299
Nikolai Samteladze Avatar asked Nov 08 '13 18:11

Nikolai Samteladze


People also ask

How do I find the base URL of a controller?

How to Get the Base URL in an MVC Controller. Here's a simple one-liner to get the job done. var baseUrl = string. Format(“{0}://{1}{2}”, Request.

What is base class in Web API controller?

Pretty much all Web API 2 controllers inherit from ApiController but Controller is the base class in the new world. In MVC 6 the same Controller base class can be used whether you are writing a RESTful API or an MVC website.


1 Answers

In the action method of the request to the url "http://localhost:85458/api/ctrl/"

var baseUrl = Request.RequestUri.GetLeftPart(UriPartial.Authority) ; 

this will get you http://localhost:85458

like image 84
mLar Avatar answered Oct 14 '22 09:10

mLar