Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET core mapping subdomains to areas

I am trying to map the subdomains to areas, So far all the answers I found were for pervious versions of .NET and not .NET core. the best and most relevant answer I found was from This page. however i am having a problem implementing it as it appears to be the walkthrough for a pervious version of .NET core and i am getting the 'MvcRouteHandler' does not contain a constructor that takes 0 arguments Error.

Here is the code which i get the error from:

public class AreaRouter : MvcRouteHandler, IRouter //the error happens in this line, visual studio underlines the AreaRoute word
{
    public new async Task RouteAsync(RouteContext context)
    {
        string url = context.HttpContext.Request.Headers["HOST"];

        string firstDomain = url.Split('.')[0];
        string subDomain = char.ToUpper(firstDomain[0]) + firstDomain.Substring(1);

        string area = subDomain;

        context.RouteData.Values.Add("area", subDomain);

        await base.RouteAsync(context);
    }
}

so anyway, i am looking for another way to map subdomains to areas or find a way to fix this error.

like image 593
Soorena Aban Avatar asked Feb 17 '17 01:02

Soorena Aban


2 Answers

Edit: Complete tutorial available here. Managed to create working version of your file.
1. Code for Router is available here. Specify your own subdomains in _allowedSubdomains array on top of file.
2. After adding router, change your code in Startup.cs:

app.UseMvc(routes =>
       {
       routes.DefaultHandler = areaRouter;

areaRouter should be passed as described by @mepsi.
3. Finally, allow the use of sudomains as described here.
Everything should be working now. The complete code is available on Github. I will write complete tutorial with all explanations later.

like image 190
Andriy Vasylyk Avatar answered Sep 23 '22 14:09

Andriy Vasylyk


I'm struggling with the same issue and I managed to get forward by adding my router on ConfigureServices with:

services.AddSingleton<AreaRouter>();

Then inject it into the Configure method with:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, AreaRouter areaRouter)

And finally set it in place:

routes.DefaultHandler = areaRouter;

Fixing this problem got me forward, but unfortunetaly I still couldn't get the subdomain routing to work as intended. It seems like routing decision is already made at this point.

Note: I would have only added comment but I can't do that yet.

like image 23
mepsi Avatar answered Sep 20 '22 14:09

mepsi