代码之家  ›  专栏  ›  技术社区  ›  Sigex

默认显示为/Home的ASP.Net核心默认路由

  •  1
  • Sigex  · 技术社区  · 7 年前

    嗨,我在ASP.Net核心中使用了mapRoutes,我可以设置默认的控制器路由。启动应用程序时,入口点将是默认的主节点,但将显示的全部内容是 https://localhost:43594

    有没有办法明确执行这一点?

    2 回复  |  直到 7 年前
        1
  •  1
  •   René Marc Gravell    7 年前

    有几个解决方法:

    • 解决方法1:可以按照说明删除默认值 here
    • 解决方法2:创建一个不同的默认操作,并按照说明从中重定向到/Home/Index here
    • 解决方法3:检查当前的URL,并重定向它是否是根目录,如前所述 here
        2
  •  1
  •   Morteza Zabihi    6 年前

    要更改路由URL,可以尝试 URL Rewriting Middleware Custom Middleware .

    Startup :

    •  app.UseRewriter(new RewriteOptions().AddRewrite("/","/Home",true));
      
    • 重定向URL

       app.UseRewriter(new RewriteOptions().AddRedirect("/", "/Home"));
      
    • 自定义中间件

          app.Use(async (context, next) =>
          {
              if (context.Request.Path == "/")
              {
                  context.Response.Redirect("Home/", true);
      
                  return;
              }
              await next();
          });
      

      app.UseMvc

          app.UseMvc(routes =>
          {
              routes.MapRoute(
                  name: "default",
                  template: "{controller=Home}/{action=Index}/{id?}");
          });