【问题标题】:ASP.NET Core 2.x defaults to non-existent route [duplicate]ASP.NET Core 2.x 默认为不存在的路由 [重复]
【发布时间】:2018-10-05 02:18:10
【问题描述】:

如何在 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();
 }

【问题讨论】:

  • 为什么你甚至想要一个控制器来重定向到 swagger UI?只需按照文档中的说明注册 SwaggerUI。在那里,您可以配置可以使用 swagger ui 的端点...
  • swagger 只是一个例子,我可以在那里使用任何东西,想法仍然存在,为什么它不起作用?为什么我看到一条不应该存在的路线?

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-30
    • 2020-05-31
    • 2017-07-08
    • 2018-12-09
    • 2017-11-27
    • 1970-01-01
    • 2022-07-07
    • 2021-02-22
    相关资源
    最近更新 更多