2008-11-10 18:04:23 8 Comments
Is it possible to have an ASP.NET MVC route that uses subdomain information to determine its route? For example:
- user1.domain.com goes to one place
- user2.domain.com goes to another?
Or, can I make it so both of these go to the same controller/action with a username
parameter?
Related Questions
Sponsored Content
21 Answered Questions
[SOLVED] File Upload ASP.NET MVC 3.0
- 2011-03-04 12:42:08
- user637197
- 320305 View
- 784 Score
- 21 Answer
- Tags: c# asp.net-mvc asp.net-mvc-3
14 Answered Questions
[SOLVED] How to render an ASP.NET MVC view as a string?
- 2009-01-27 11:43:56
- Dan Atkinson
- 267558 View
- 450 Score
- 14 Answer
- Tags: asp.net-mvc rendering
15 Answered Questions
[SOLVED] Can an ASP.NET MVC controller return an Image?
- 2008-10-09 05:58:17
- Jonathan
- 206724 View
- 423 Score
- 15 Answer
- Tags: asp.net asp.net-mvc image controller
20 Answered Questions
[SOLVED] How can I get my webapp's base URL in ASP.NET MVC?
- 2009-08-17 13:47:41
- Benjamin Pollack
- 256362 View
- 268 Score
- 20 Answer
- Tags: .net asp.net asp.net-mvc iis
1 Answered Questions
[SOLVED] Routing based on subdomain to areas while keeping same URL parameters
- 2017-10-10 04:45:35
- BinaryBoss
- 536 View
- 2 Score
- 1 Answer
- Tags: asp.net-mvc model-view-controller url-routing asp.net-mvc-routing
6 Answered Questions
[SOLVED] How can I get the client's IP address in ASP.NET MVC?
- 2010-04-05 08:25:17
- melaos
- 210926 View
- 285 Score
- 6 Answer
- Tags: asp.net-mvc
3 Answered Questions
[SOLVED] Routing with Multiple Parameters using ASP.NET MVC
- 2010-02-11 17:59:51
- CodingWithoutComments
- 243789 View
- 125 Score
- 3 Answer
- Tags: c# .net asp.net-mvc routing
1 Answered Questions
[SOLVED] ASP.Net MVC Allow Subdomains in URL
- 2013-11-23 05:29:34
- user1445086
- 142 View
- 0 Score
- 1 Answer
- Tags: c# asp.net-mvc
1 Answered Questions
[SOLVED] How can we make an ASP.NET MVC4 route based on a subdomain?
- 2013-01-31 17:52:14
- Houshang.Karami
- 2185 View
- 0 Score
- 1 Answer
- Tags: asp.net-mvc asp.net-mvc-4 url-rewriting routing
0 Answered Questions
Is it possible to manipulate routing so it looks like a subdomain in mvc .net
- 2012-05-16 18:17:42
- Andreas Gustafsson
- 169 View
- 1 Score
- 0 Answer
- Tags: asp.net-mvc routing subdomain
10 comments
@Jean 2019-01-22 16:52:47
Few month ago I have developed an attribute that restricts methods or controllers to specific domains.
It is quite easy to use:
You can also apply it directly on a controller.
Restriction: you may not be able to have two same routes on different methods with different filters I mean the following may throw an exception for duplicate route:
@Mariusz 2017-10-17 17:08:17
I created library for subdomain routing which you can create such a route. It is working currently for a .NET Core 1.1 and .NET Framework 4.6.1 but will be updated in near future. This is how is it working:
1) Map subdomain route in Startup.cs
2) Controllers/HomeController.cs
3) That lib will also allow you to generate URLs and forms. Code:
Will generate
<a href="http://user1.localhost:54575/Home/Index">User home</a>
Generated URL will also depend on current host location and schema.You can also use html helpers for
BeginForm
andUrlHelper
. If you like you can also use new feature called tag helpers (FormTagHelper
,AnchorTagHelper
)That lib does not have any documentation yet, but there are some tests and samples project so feel free to explore it.
@Edward Brey 2016-10-12 10:34:08
In ASP.NET Core, the host is available via
Request.Host.Host
. If you want to allow overriding the host via a query parameter, first checkRequest.Query
.To cause a host query parameter to propagate into to new route-based URLs, add this code to the
app.UseMvc
route configuration:And define
HostPropagationRouter
like this:@Darxtar 2016-10-05 22:35:37
If you are looking at giving MultiTenancy capabilities to your project with different domains/subdomains for each tenant, you should have a look at SaasKit:
https://github.com/saaskit/saaskit
Code examples can be seen here: http://benfoster.io/blog/saaskit-multi-tenancy-made-easy
Some examples using ASP.NET core: http://andrewlock.net/forking-the-pipeline-adding-tenant-specific-files-with-saaskit-in-asp-net-core/
EDIT: If you do no want to use SaasKit in your ASP.NET core project you can have a look at Maarten's implementation of domain routing for MVC6: https://blog.maartenballiauw.be/post/2015/02/17/domain-routing-and-resolving-current-tenant-with-aspnet-mvc-6-aspnet-5.html
However those Gists are not maintained and need to be tweaked to work with the latest release of ASP.NET core.
Direct link to the code: https://gist.github.com/maartenba/77ca6f9cfef50efa96ec#file-domaintemplateroutebuilderextensions-cs
@Dan Esparza 2016-10-07 13:48:03
Not looking for multitenancy -- but thanks for the tip!
@Amirhossein Mehrvarzi 2015-06-19 08:10:33
After defining a new Route handler that would look at the host passed in the URL, you can go with the idea of a base Controller that is aware of the Site it’s being accessed for. It looks like this:
ISiteProvider
is a simple interface:I refer you go to Luke Sampson Blog
@Edward Brey 2013-09-10 19:33:46
To capture the subdomain when using Web API, override the Action Selector to inject a
subdomain
query parameter. Then use the subdomain query parameter in your controllers' actions like this:This approach makes debugging convenient since you can specify the query parameter by hand when using localhost instead of the actual host name (see the standard MVC5 routing answer for details). This is the code for Action Selector:
Replace the default Action Selector by adding this to
WebApiConfig.Register
:@Alan Macdonald 2016-04-07 10:23:33
Anyone having issues where the route data doesn't appear on the web API controller and inspecting the Request.GetRouteData inside the controller is showing no values?
@Edward Brey 2013-03-08 05:50:33
To capture the subdomain while retaining the standard MVC5 routing features, use the following
SubdomainRoute
class derived fromRoute
.Additionally,
SubdomainRoute
allows the subdomain optionally to be specified as a query parameter, makingsub.example.com/foo/bar
andexample.com/foo/bar?subdomain=sub
equivalent. This allows you to test before the DNS subdomains are configured. The query parameter (when in use) is propagated through new links generated byUrl.Action
, etc.The query parameter also enables local debugging with Visual Studio 2013 without having to configure with netsh or run as Administrator. By default, IIS Express only binds to localhost when non-elevated; it won't bind to synonymous hostnames like sub.localtest.me.
For convenience, call the following
MapSubdomainRoute
method from yourRegisterRoutes
method just as you would plain oldMapRoute
:Finally, to conveniently access the subdomain (either from a true subdomain or a query parameter), it is helpful to create a Controller base class with this
Subdomain
property:@Edward Brey 2013-11-03 03:32:54
I updated the code to make the subdomain always available as a route value. This simplifies access to the subdomain.
@SoonDead 2014-02-25 14:22:14
I like this. Very simple, and more than enough for my project.
@perfect_element 2017-01-27 04:42:34
This is a great answer. Is there a way for this to work with route attributes? I'm trying to make this work for paths like "subdomain.domain.com/portal/register" and using attributes would make this easier.
@NightOwl888 2017-10-10 11:44:03
@perfect_element - Attribute routes are not extensible like convention based routes are. The only way to do something like that would be to build your own attribute routing system.
@Jim Blake 2010-04-27 18:22:52
This is not my work, but I had to add it on this answer.
Here is a great solution to this problem. Maartin Balliauw wrote code that creates a DomainRoute class that can be used very similarly to the normal routing.
http://blog.maartenballiauw.be/post/2009/05/20/ASPNET-MVC-Domain-Routing.aspx
Sample use would be like this...
;
@Gorkem Pacaci 2010-06-28 20:12:16
There is a problem with this solution. Say, you want to handle subdomains as different users: routes.Add("SD", new DomainRoute("user}.localhost", "", new { controller = "Home", action = "IndexForUser", user="u1" } )); It caches the homepage as well. This is because of the regex that's generated. In order to fix this, you can make a copy of the CreateRegex method in DomainRoute.cs, name it CreateDomainRegex, change the * on this line to +: source = source.Replace("}", @">([a-zA-Z0-9_]*))"); and use this new method for domain regx in GetRouteData method: domainRegex = CreateDomainRegex(Domain);
@Dr TJ 2014-08-05 12:02:58
I don't know why I can't run this code... I just receive
SERVER NOT FOUND
error... means the code is not working for me... are you setting any other configuration or something?!@IDisposable 2014-11-19 20:25:09
I've created a Gist of my version of this gist.github.com/IDisposable/77f11c6f7693f9d181bb
@HaBo 2014-11-24 03:39:17
@IDisposable what is MvcApplication.DnsSuffix?
@IDisposable 2014-11-25 05:37:08
We just expose the base DNS domain in web.config... typical value would be .example.org
@Jon Cahill 2009-02-12 14:30:49
You can do it by creating a new route and adding it to the routes collection in RegisterRoutes in your global.asax. Below is a very simple example of a custom Route:
@justSteve 2012-01-04 17:25:47
Thanks for the detailed sample but I'm not following how to execute the .Add from Global.asax.
@Jeff Handley 2012-02-01 09:58:48
I called the route SubdomainRoute and added it as the first route like this: routes.Add(new SubdomainRoute());
@Maxim V. Pavlov 2012-03-31 08:15:28
Does this approach requires hard-coding a list of possible sub-domains?
@Ryan Hayes 2012-11-25 23:50:32
No, you can add a database field called something like "subdomain" that you will be what you're expecting the subdomain to be for a particular user, or whatever else, then just do a lookup on the subdomain.
@MatthewT 2014-03-01 01:44:15
Could anybody recommend a webforms version of this?
@JoshYates1980 2016-02-02 20:13:11
This guy has a better approach: benjii.me/2015/02/subdomain-routing-in-asp-net-mvc
@shajeer puzhakkal 2017-12-12 12:59:56
Please recommend for Web Api
@Nick Berardi 2008-11-10 18:13:16
Yes but you have to create your own route handler.
Typically the route is not aware of the domain because the application could be deployed to any domain and the route would not care one way or another. But in your case you want to base the controller and action off the domain, so you will have to create a custom route that is aware of the domain.