首页 > 解决方案 > ASP.NET Core 2.x 默认为不存在路由

问题描述

如何更改 ASP.NET Core 中的默认路由

路线很奇怪其实是

api/值

它不再存在,我已经删除了控制器,就是这样,我刚刚创建了一个 Home 控制器,其余的都是 API。为什么还有这条路线存在?

这是我想默认的控制器

 public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return new RedirectResult("~/swagger");
        }
    }

我试过这个答案

.net Core - 使用 Route 属性时未加载默认控制器

但对我不起作用

这是我的配置

public void ConfigureServices(IServiceCollection services)
{
    .AddMvc(options =>
            {
            })
            .AddControllersAsServices()
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_1); 
}


 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
 {
       app.UseMvcWithDefaultRoute();
 }

标签: c#asp.net-core.net-coreasp.net-core-mvcasp.net-core-2.0

解决方案


属性下有一个launchSettings.json 。您可以使用默认路由更改launchUrl字段。

{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false, 
    "anonymousAuthentication": true, 
    "iisExpress": {
      "applicationUrl": "http://localhost:63986",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "api/values", //change here
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "WebApplication1": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "api/values", //change here
      "applicationUrl": "http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

推荐阅读