ASP.NET核心将子域映射到区域 [英] ASP.NET core mapping subdomains to areas

查看:71
本文介绍了ASP.NET核心将子域映射到区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将子域映射到区域,到目前为止,我发现的所有答案都是针对.NET的以前版本,而不是.NET core.我发现的最佳和最相关的答案来自此页面.但是,我在实现它时遇到了问题,因为它似乎是.NET Core以前版本的演练,并且出现了'MvcRouteHandler' does not contain a constructor that takes 0 arguments错误.

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.

推荐答案

完整的教程可用此处获得路由器代码.在文件顶部的_allowedSubdomains数组中指定您自己的子域.
2.添加路由器后,在Startup.cs中更改代码:

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应该按照@mepsi的说明进行传递.
3.最后,按照此处所述,允许使用sudomain.
现在一切都应该正常工作.完整的代码可在 Github 上找到.稍后,我将编写完整的教程并提供所有解释.

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.

这篇关于ASP.NET核心将子域映射到区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆