本文介绍了启动ASP .NET Core上的MapSpaFallbackRoute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 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 的正确方法是什么,我想在Home区域上使用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

这是我完整的代码,我想使用路径admin请求,管理区域上的控制器应处理该问题。

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.

现在您的问题了:是的, {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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 06:36