Mapping Area to Subdomain ASP.NET Core 7

huangapple go评论54阅读模式
英文:

Mapping Area to Subdomain ASP.NET Core 7

问题

I'm trying to Map areas I've created to subdomains and failing.

我正在尝试将我创建的区域映射到子域名,但失败了。

I have an area named "sugar" and I'm using a SubDomainRouteTransformer class I found online that worked for an ASP.NET core 5 project with the code:

我有一个名为"sugar"的区域,我正在使用我在网上找到的SubDomainRouteTransformer类,它适用于一个包含以下代码的ASP.NET Core 5项目:


public class SubDomainRouteTransformer : DynamicRouteValueTransformer
{
    public override async ValueTask<RouteValueDictionary> TransformAsync(HttpContext httpContext, RouteValueDictionary values)
    {
        var subDomain = httpContext.Request.Host.Host.Split(".").First();
        if (string.IsNullOrEmpty(subDomain))
        {
            values["controller"] = subDomain;
        }
        return values;
    }
}

in my program.cs file I implement it thus

builder.Services.TryAddTransient<SubDomainRouteTransformer>();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
    
app.MapDynamicControllerRoute<SubDomainRouteTransformer>("Sugar", "{area:exists}/{controller=Home}/{action=Index}/{id?}");

However, whenever I try accessing the main website, which is the one under the "default" route, I keep getting directed to the "sugar" area. Is there a proper way I can implement this?

然而,每当我尝试访问主网站,即位于"default"路由下的网站时,我总是被重定向到"sugar"区域。我是否可以正确实现这个功能的方法?

英文:

I'm trying to Map areas I've created to subdomains and failing.

I have an area named "sugar" and I'm using a SubDomainRouteTransformer class I found online that worked for an ASP.NET core 5 project with the code:


    

    public class SubDomainRouteTransformer : DynamicRouteValueTransformer
    {
        public override async ValueTask&lt;RouteValueDictionary&gt; TransformAsync(HttpContext httpContext, RouteValueDictionary values)
        {
            var subDomain = httpContext.Request.Host.Host.Split(&quot;.&quot;).First();
            if (string.IsNullOrEmpty(subDomain))
            {
                values[&quot;controller&quot;] = subDomain;
            }
            return values;
        }
    }

in my program.cs file I implement it thus

    builder.Services.TryAddTransient&lt;SubDomainRouteTransformer&gt;();

    app.MapControllerRoute(
        name: &quot;default&quot;,
        pattern: &quot;{controller=Home}/{action=Index}/{id?}&quot;);
    
    app.MapDynamicControllerRoute&lt;SubDomainRouteTransformer&gt;(&quot;Sugar&quot;, &quot;{area:exists}/{controller=Home}/{action=Index}/{id?}&quot;);

However, whenever I try accessing the main website, which is the one under the "default" route, I keep getting directed to the "sugar" area. Is there a proper way I can implement this?

答案1

得分: 2

Firstly,with your codes,"Sugar" would be considered as the route pattern,you have to remove "Sugar"

Secondly

> whenever I try accessing the main website, which is the one under the
> "default" route, I keep getting directed to the "sugar" area

check if subdomain exists:

var host = httpContext.Request.Host.Host;
var subDomain = host.Split(".").Count() > 1 ? host.Split(".").First() : "";

If you don't want to be directed to Sugar area when subdomain doesn't exist, try

if (!string.IsNullOrEmpty(subDomain))
{
  values["controller"] = subDomain;
  values["area"] = subDomain;
}
else
{
   values.Remove("area");
 }

I tried with a minimal example:

in program.cs:

app.MapDynamicControllerRoute<SubDomainRouteTransformer>("{area}/{controller=Home}/{action=Index}/{id?}");

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

SubDomainRouteTransformer:

public class SubDomainRouteTransformer : DynamicRouteValueTransformer
    {
        public override async ValueTask<RouteValueDictionary> TransformAsync(HttpContext httpContext, RouteValueDictionary values)
        {
            var host = httpContext.Request.Host.Host;
            var subDomain = host.Split(".").Count() > 1 ? host.Split(".").First() : "";
            if (!string.IsNullOrEmpty(subDomain))
            {
                values["controller"] = subDomain;
                values["area"] = subDomain;
            }
            else
            {
                values.Remove("area");
            }
            return values;
        }
    }

Result:

英文:

Firstly,with your codes,"Sugar" would be considered as the route parttern,you have to remove "Sugar"

Mapping Area to Subdomain ASP.NET Core 7

Secondly

> whenever I try accessing the main website, which is the one under the
> "default" route, I keep getting directed to the "sugar" area

check if subdomain exists:

var host = httpContext.Request.Host.Host;
var subDomain = host.Split(&quot;.&quot;).Count() &gt; 1 ? host.Split(&quot;.&quot;).First() : &quot;&quot;;

If you don't want to be directed to Sugar area when subdomain doesn't exists,try

if (!string.IsNullOrEmpty(subDomain))
{
  values[&quot;controller&quot;] = subDomain;
  values[&quot;area&quot;] = subDomain;
}
else
{
   values.Remove(&quot;area&quot;);
 }

I tried with a minimal example:

Mapping Area to Subdomain ASP.NET Core 7

Mapping Area to Subdomain ASP.NET Core 7

in program.cs:

app.MapDynamicControllerRoute&lt;SubDomainRouteTransformer&gt;(&quot;{area}/{controller=Home}/{action=Index}/{id?}&quot;);

app.MapControllerRoute(
    name: &quot;default&quot;,
    pattern: &quot;{controller=Home}/{action=Index}/{id?}&quot;);

SubDomainRouteTransformer:

public class SubDomainRouteTransformer : DynamicRouteValueTransformer
    {
        public override async ValueTask&lt;RouteValueDictionary&gt; TransformAsync(HttpContext httpContext, RouteValueDictionary values)
        {
            var host = httpContext.Request.Host.Host;
            var subDomain = host.Split(&quot;.&quot;).Count() &gt; 1 ? host.Split(&quot;.&quot;).First() : &quot;&quot;;
            if (!string.IsNullOrEmpty(subDomain))
            {
                values[&quot;controller&quot;] = subDomain;
                values[&quot;area&quot;] = subDomain;
            }
            else
            {
                values.Remove(&quot;area&quot;);
            }
            return values;
        }
    }

Result:

Mapping Area to Subdomain ASP.NET Core 7

Mapping Area to Subdomain ASP.NET Core 7

huangapple
  • 本文由 发表于 2023年4月20日 09:53:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76059981.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定