【问题标题】:ASP.NET Core MVC doesn't launch on default routeASP.NET Core MVC 不会在默认路由上启动
【发布时间】:2022-07-07 14:56:46
【问题描述】:

由于某些奇怪的原因,它启动到 https://localhost:44397/index.html 而不是转到我的家庭控制器中的 Index 方法。我在 wwwroot 中没有 index.html 文件,所以我不知道为什么会发生这种奇怪的行为。

我只想有正常的行为,当 URL 像这样 https://localhost:44397/ 时直接启动到索引。

如果这有帮助,我还有其他一些 API 控制器,但是我之前已经做过很多次了,所以我不怀疑这是导致此问题的原因。我还尝试创建一个新的 MVC 项目并直接运行,并且库存模板工作正常。任何帮助将不胜感激,谢谢。

我有一个看起来像这样的家庭控制器

 public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }

我的配置方法

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider serviceProvider)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();

            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();
            app.UseSession();
            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });

           // CreateRoles(serviceProvider);
        }

launchSetting.json 文件

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:64858",
      "sslPort": 44397
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
      }
    },
    "IcartE1": {
      "commandName": "Project",
      "dotnetRunMessages": "true",
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation"
      }
    }
  }
}

【问题讨论】:

  • 你是如何开始你的项目的?
  • IIS Express @MaartenDev
  • 等待你使用 IIS 管理工具的打开操作吗?还是从 Visual Studio 运行它?
  • 我从 Visual Studio @MaartenDev 运行
  • 您是否尝试配置IIS Express 配置文件的launchUrl 属性?

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


【解决方案1】:

首先需要定义AddControllers的依赖注入,所以在ConfigureServices方法中加入这一行:

services.AddControllers();

之后,将此路由添加到 Endpoints 中间件参数:

app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller}/{action}/{id?}",
            defaults: new { controller = "Home", action = "Index" });
            endpoints.MapControllers();
        });

【讨论】:

    【解决方案2】:

    有同样的问题,对我来说是缓存重定向网址的浏览器。因此,如果您按 f12 并选择(空缓存和硬重载)。 In 将清除它并停止将您路由到 index.html

    请通读此 https://superuser.com/questions/304589/how-can-i-make-chrome-stop-caching-redirects

    【讨论】:

      猜你喜欢
      • 2017-07-08
      • 2021-12-18
      • 2010-11-01
      • 2020-05-31
      • 2018-12-09
      • 2019-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多