Omar Hossam
Omar Hossam

Reputation: 11

ASP.NET Core MVC doesn't launch on default route

For some weird reason, it launches to https://localhost:44397/index.html instead of going to the Index method in my home controller. I don't have an index.html file in wwwroot so I don't know why this weird behaviour is occuring.

I just want to have normal behaviour which is launch directly to Index when URL is like this https://localhost:44397/ .

I have some other API controllers if this helps however I've done it numerous times before so I don't suspect it's what's causing this issue.I also tried creating a new MVC project and running directly and stock template worked fine. Any help would be immensely appreciated, thanks.

I have a home controller which looks like this

 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 });
        }
    }

my Configure method

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 file

{
  "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"
      }
    }
  }
}

Upvotes: 0

Views: 1545

Answers (3)

Gaotter
Gaotter

Reputation: 1906

Had the same problem, for me it was the browser that caches the redirect urls. So if you push f12 and select the (Empty Cache and Hard Reload). It will clear it and stop routing you to index.html

ore read through this https://superuser.com/questions/304589/how-can-i-make-chrome-stop-caching-redirects

enter image description here

Upvotes: 1

Ozan BAYRAM
Ozan BAYRAM

Reputation: 2901

Looks like the problem I had, about browser caching

You may check the answers from this link: Weird redirection problem with ASP.NET Core 6 Razor pages application

Upvotes: 0

Pouria Montakhabi
Pouria Montakhabi

Reputation: 255

First of all, you need to define AddControllers dependency injection, so in ConfigureServices method add this line:

services.AddControllers();

After that, add this route to Endpoints middleware parameters:

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

Upvotes: 0

Related Questions