Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get my webapp's base URL in ASP.NET MVC?

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.

like image 649
Benjamin Pollack Avatar asked Aug 17 '09 13:08

Benjamin Pollack


People also ask

What is default URL in MVC?

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.

How can I get the baseUrl of my site in asp net core?

var baseUrl = Request. GetTypedHeaders(). Referer. ToString();

How do I change the default URL in MVC?

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.


2 Answers

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 
like image 113
tghw Avatar answered Oct 03 '22 23:10

tghw


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}"; } 
like image 34
Serj Sagan Avatar answered Oct 04 '22 00:10

Serj Sagan