我有一个 ASP.NET Core MVC Web 应用程序,其中 ASP.NET Core Identity 位于名为
Identity
的区域内,而主 Web 应用程序没有区域。除此之外,我想要一组自定义前端页面,其中包含 npm 包以及我不想与主应用程序混合的 JS 和 CSS 文件。所以我想要另一个叫做 Landing
的区域。这将是一个无限滚动的页面,该区域不需要安全性。
我的
program.cs
里有这个:
app.UseEndpoints(endpoints =>
{
endpoints.MapAreaControllerRoute(name: "Identity", areaName: "Identity", pattern: "Identity/{controller=Home}/{action=Index}");
endpoints.MapControllerRoute("default", "{controller=Dashboard}/{action=DashboardV3}");
endpoints.MapRazorPages();
});
因为我无法弄清楚
new app.MapGet("/", () => "Hello World!");
语法。
问题:
问题是,如果我访问根 URL(例如
localhost:20202/
或 myap.com/
),我会收到错误/404 页面。我必须在身份区域下或基本应用程序中显式点击控制器。
现阶段我不确定将所有内容移动到区域是否明智,但我现在必须添加着陆区域,但我的路由未按预期工作。我假设如果用户只是转到
localhost:20202
,他将被重定向到默认页面(控制器/操作),该页面将被标记为已授权,并且他将被发送到 Identity/*/Login。但我必须明确指定基本应用程序的 url 为:localhost:20202/Revenue/AssetList
才能触发所有内容。
必填
/Landing/Home/index.html
/Revenue/Dashboard
(无区域)/Identity/*/Login
这就是我想要在路线中映射的内容。
问题是,如果我转到根 URL,例如 localhost:20202/ 或 myap.com/,我收到一个 error/404 页面。我必须明确地点击 控制器位于身份区域下或基本应用程序中。
根据您的场景,这就是问题所在,因为您的登陆区域是一组自定义前端页面,其中包含 npm 包和吞咽的 JS 和 CSS 文件,其中大部分是静态文件,因此您不需要控制器路由。
相反,您应该为登陆区域的资产(例如 HTML、CSS 和 JS)配置静态文件服务。使用 MapFallbackToDirectory 以及您的登陆文件的适当目录路径。
例如,您可以执行以下操作:
endpoints.MapAreaFallbackToDirectory(name: "Landing", areaName: "Landing", directory: "Landing/dist");
然后配置你的静态文件资源:
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "Landing")
),
RequestPath = "/Landing"
});
注意:检查静态文件配置。
因此,您的身份、仪表板和登陆页面的路线配置总共应如下所示:
app.UseEndpoints(endpoints =>
{
endpoints.MapAreaControllerRoute(name: "Identity", areaName: "Identity", pattern: "Identity/{controller=Home}/{action=Index}");
endpoints.MapControllerRoute(
name: "landing",
pattern: "Landing/{controller=Home}/{action=Index}"
);
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Dashboard}/{action=DashboardV3}"
);
endpoints.MapRazorPages();
endpoints.MapFallbackToFile("/Landing/{**slug}", "Landing/Home/index.html");
});
注意:“/Landing”是文字路径前缀。以“/Landing”开头的请求将匹配此路由。 请记住,{**slug} 参数占位符会将 URL 的其余部分捕获为名为“slug”的单个参数。你可以参考这个官方文档。