启动 ASP .NET Core 上的 MapSpaFallbackRoute [英] MapSpaFallbackRoute on Startup ASP .NET Core
问题描述
我在 Net Core App
上有一个名为 Admin
的区域,在启动时的 MapSpaFallbackRoute
设置中,我想这样设置,>
I have an Areas on Net Core App
named Admin
, on MapSpaFallbackRoute
setting on startup, I want to set like this,
app.UseMvc(routes =>
{
routes.MapSpaFallbackRoute(
name: "spa-fallback-admin",
defaults: new { area="Admin", controller = "Home", action = "Index" });
});
这是定义 MapSpaFallbackRoute
的正确方法吗?我怀疑 MapSpaFallbackRoute
是否有属性 area,我一直在尝试这个,我的应用程序返回 404(未找到).那么,定义 MapSpaFallbackRoute
的正确方法是什么,我想在管理区域使用 HomeController,并带有索引操作
is this the correct way to define MapSpaFallbackRoute
? I doubt MapSpaFallbackRoute
have attributes area,
I have been try this, and my apps return 404(not found).
so, what the correct way to define MapSpaFallbackRoute
, I want using HomeController on Admin area, with Index action
这是我的完整代码,我想用路径管理请求,管理区域的控制器应该处理.
It is my complete code, I want to request with path admin, controller on admin areas should be handle that.
app.MapWhen(context => context.Request.Path.Value.StartsWith("/admin"), builder =>
{
builder.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{area=Admin}/{controller=Home}/{action=Index}/{id?}");
routes.MapSpaFallbackRoute(
name: "spa-fallback-admin",
defaults: new { area="Admin", controller = "Home", action = "Index" });
});
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapSpaFallbackRoute(
name: "spa-fallback",
defaults: new { controller = "Home", action = "Index" });
routes.MapRoute(
name: "areas",
template: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
});
感谢您的帮助
推荐答案
MapSpaFallbackRoute
所做的是允许定义路由参数的默认值以处理 404 情况.
What MapSpaFallbackRoute
does is allows defining default values for route parameters to handle 404 cases.
现在回答您的问题:是的,MVC 路由(属性/约定)支持 {area}
作为路由参数,因此您可以编写上面的代码来定义默认值.
Now to your question: yes, MVC routing (both attribute/convention) supports {area}
as route parameter and so you can write above code to define a default value.
你没有显示你的路由设置,所以我认为你的主要问题是你没有在路由模板中指定 {area}
参数.
You didn't show your routing setup, so I assume that your main problem is that you haven't specified {area}
parameter in your route template.
例如,如果考虑约定路由,以下应该有效:
For example, if consider convention routing, the following should work:
app.UseMvc(routes => {
routes.MapRoute("default", "{area}/{controller}/{action}/{id?}");
routes.MapSpaFallbackRoute(
name: "spa-fallback-admin",
defaults: new { area="Admin", controller = "Home", action = "Index" });
});
<小时>
对于更新的问题:尝试使用 .UseWhen
而不是 .MapWhen
:
app.UseWhen(context => context.Request.Path.Value.StartsWith("/admin"), builder =>
{
这篇关于启动 ASP .NET Core 上的 MapSpaFallbackRoute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!