英文:
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<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?
答案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"
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 exists,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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论