2
Answers

i want to get base url in WebApiConfig in mvc application

Sharad Gupta

Sharad Gupta

7y
134
1
Hi,
 
i want to get base url in WebApiConfig in mvc application, where my application is running like 'http://www.abc.com', 'http://localhost:45678'
  eyc. 
Answers (2)
0
Sharad Gupta

Sharad Gupta

NA 19.6k 4.3m 7y
Hi sagar,
thanks for you time, but you got execption when you try in your webapi.config, beacuse it not find out httpcontext.current, so if you have another code then please check it first, in your testing project. and Thanks in adavance. 
0
Sagar  Pandurang Kap

Sagar Pandurang Kap

NA 2.7k 7.5k 7y
Hi,
 
Try below code :-
 
Assuming you have a Request object available, you can use:
 
string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~"));
 
If it's not available, you can get to it via the context:
 
var request = HttpContext.Current.Request;
 
Hope it helps......