Fazle Rabbi Tanjil
Fazle Rabbi Tanjil

Reputation: 236

ASP MVC routing URL translating into a a sub domain breaks overall Site. Need suggestions how to do that

I have an asp.net mvc 5 application. This application used asp.net mvc 5 attribute routing.

In all routing I have a top level Route prefix like "/myapp/org_code/restoftheroutes/ for example: https://myapp.com/apple/register/112 https://myapp.com/google/register/112

Here the org_code is route parameter.

Now as per client request i have to put different org_code into sub domain.

Like : https://myapp.com/apple/register/112 will translate to: https://apple.myapple.com/register/112

In the dns i have pointed https://apple.myapple.com to a https://myapp.com/apple but doing that my static contents like css and images does't load as the root of the app becomes https://myapp.com/apple instead of https://myapp.com

Please give me a suggestion how to resolve this.

Upvotes: 0

Views: 70

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239280

Unfortunately, MVC 5 Attribute Routing does not support subdomains. However, the AttributeRouting Nuget package it is based on does. Fortunately, you can use both simultaneously in your project, though only one or the other per controller.

The way the AttributeRouting Nuget handles subdomains is via Areas, so you will have to create an Area in your project for each of the subdomains you want to map. That may seem problematic, but you can still share much of the code between the areas, including views, since Razor will always fallback to the root Views folder to search for a view. The only thing you will have to create specific to the area is one or more controllers, but these controllers can inherit from a common base controller, allowing you to share the actual controller code. For more information on subdomain routing via the AttributeRouting Nuget, see the documentation.

If you don't want to go AttributeRouting Nuget route, your only other option is standard MVC routing. You essentially just set up a custom route constraint, that will add your subdomain into the route values dictionary. You can then use this to make decisions in your actions accordingly. Just do a search for "asp.net mvc subdomain routing"; there's a tons of different examples.

Upvotes: 1

Related Questions