ASP.NET Core 2.x默认为不存在的路由

时间:2018-10-04 16:30:52

标签: c# asp.net-core .net-core asp.net-core-mvc asp.net-core-2.0

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

路线很奇怪,实际上是

  

api /值

已经不存在了,我已经删除了控制器,仅此而已, 我刚刚创建了一个Home控制器,其余的都是API。 为什么仍然存在此路线?

这是我要默认设置为

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

我已经尝试过这个答案

.net Core - Default controller is not loading when Route attribute is used

但不适用于我

这是我的配置

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


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

1 个答案:

答案 0 :(得分:2)

属性下有一个 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"
      }
    }
  }
}