How can I quickly determine what the root URL is for my ASP.NET MVC application? I.e., if IIS is set to serve my application at http://example.com/foo/bar, then I'd like to be able to get that URL in a reliable way that doesn't involve getting the current URL from the request and chopping it up in some fragile way that breaks if I re-route my action.
The reason that I need the base URL is that this web application calls another one that needs the root to the caller web application for callback purposes.
As you can see in the above figure, the route is configured using the MapRoute() extension method of RouteCollection , where name is "Default", url pattern is "{controller}/{action}/{id}" and defaults parameter for controller, action method and id parameter.
var baseUrl = Request. GetTypedHeaders(). Referer. ToString();
You can set up a default route: routes. MapRoute( "Default", // Route name "", // URL with parameters new { controller = "Home", action = "Index"} // Parameter defaults ); Just change the Controller/Action names to your desired default.
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
So none of the ones listed here worked for me, but using a few of the answers, I got something working:
public string GetBaseUrl() { var request = HttpContext.Current.Request; var appUrl = HttpRuntime.AppDomainAppVirtualPath; if (appUrl != "/") appUrl = "/" + appUrl; var baseUrl = string.Format("{0}://{1}{2}", request.Url.Scheme, request.Url.Authority, appUrl); return baseUrl; }
Update for ASP.NET Core / MVC 6:
ASP.NET Core
makes this process a bit more painful, especially if you are deep in your code. You have 2 options to get at the HttpContext
1) Pass it in from your controller
:
var model = new MyClass(HttpContext);
then in model
:
private HttpContext currentContext; public MyClass(HttpContext currentContext) { this.currentContext = currentContext; }
2) Perhaps the cleaner way is to inject it into your class, which starts with registering the types in your Startup:
public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddMvc(); services.AddTransient<MyClass, MyClass>(); services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>(); }
then have it injected for you like this:
private HttpContext currentContext; public MyClass(IHttpContextAccessor httpContextAccessor) { currentContext = httpContextAccessor.HttpContext; }
in either case, here is the updated for .NET Core
GetBaseUrl()
:
public string GetBaseUrl() { var request = currentContext.Request; var host = request.Host.ToUriComponent(); var pathBase = request.PathBase.ToUriComponent(); return $"{request.Scheme}://{host}{pathBase}"; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With