.net core 3.0 路由及区域路由与默认首页的配置

时间:2024-04-17 13:47:57

Startup文件配置

public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }
 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                        name: "areas",
                        pattern: "{area:exists}/{controller=Default}/{action=Index}/{id?}"
                        );
            });
        }

Controller页面

[Area("SqlFrame")]
public class DefaultController : Controller
{
public IActionResult Index()
{
return View();
}
}

 

默认页面配置方案1

在Properties下的launchSettings.json中

 "launchUrl": "SqlFrame",  设置访问的区域
{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:50114",
      "sslPort": 44345
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "SqlFrame",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "BaseCore": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://localhost:5001;http://localhost:5000"
    }
  }
}

或者是右键解决方案 属性 

 

2、默认页面 使用管道控制页面跳转

 app.UseEndpoints(endpoints =>
            {
                endpoints.Map("/",context =>
                {
                    context.Response.Redirect("/SqlFrame/Default/Index");
                    return Task.CompletedTask;
                });

                endpoints.MapControllerRoute(
                        name: "ss",
                        pattern: "{area:exists}/{controller}/{action}/{id?}"
                        );
            });