5

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.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Soorena Aban
  • 710
  • 2
  • 10
  • 18

3 Answers3

7

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.

1

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.

mepsi
  • 101
  • 6
0

If you look at the definition of MvcRouteHandler you will see it does not have a parameterless constructor, the ones availables are:

public MvcRouteHandler(IActionInvokerFactory actionInvokerFactory, IActionSelector actionSelector, DiagnosticSource diagnosticSource, ILoggerFactory loggerFactory);
public MvcRouteHandler(IActionInvokerFactory actionInvokerFactory, IActionSelector actionSelector, DiagnosticSource diagnosticSource, ILoggerFactory loggerFactory, IActionContextAccessor actionContextAccessor);

As you're inheriting from it, you must call the base constructor, so usually you would create a constructor with the same parameters and pass it to the base as you don't require any extra parameter:

    public AreaRouter (IActionInvokerFactory actionInvokerFactory,
    IActionSelector actionSelector, DiagnosticSource diagnosticSource,
    ILoggerFactory loggerFactory, IActionContextAccessor actionContextAccessor)
    : base(actionInvokerFactory, actionSelector, diagnosticSource,
    loggerFactory, actionContextAccessor) { }
Gusman
  • 14,905
  • 2
  • 34
  • 50
  • now I am getting an error `'MvcRouteHandler' does not contain a constructor that takes 6 arguments` from the fourth line of the code you written. beside while the error might go away I will no longer be able to follow the instructions of the page I got this code from as I have no idea what values to pass to the AreaRouter in `routes.DefaultHandler = new AreaRouter();` in the startup.cs according to the instruction. and I will be getting a new error there – Soorena Aban Feb 17 '17 at 01:47
  • About the first error, that's then because the version of .net Core you have differs from mine, just right click `MvcRouteHandler` and then select "Go to definition", it will open a meta from the class and show you the functions of the class (and the constructors). And about not being available to use these instructions, that's another different question, solve first this error and then open a new question. – Gusman Feb 17 '17 at 01:50
  • well I am not fixated in doing the mapping this way, any other way that would help me map subdomains to areas is just fine. – Soorena Aban Feb 17 '17 at 01:54
  • @soorena12 remove the 'diagnosticSource' parameter, it's added two times hence not working. use it to one times – Balaji Marimuthu Feb 17 '17 at 03:27