Search code examples
c#asp.net-mvc-routing.net-7.0

.net 7. Does not see route for a controller


I'm testing .net7 MVC. I have 2 controllers. 1st - default, created during installation and 2nd, copied from the 1st and modified. 1st controller works as expected, 2nd - does not. I've broken up my head dealing with this silly case. Attaching the code

Route https://localhost:44425/account/123 - DOES NOT WORK (Return 404)

Route https://localhost:44425/weatherforecast/123 - WORKS

program.cs

using POCs.Services.Impelemtation;
using POCs.Services.Interfaces;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllersWithViews();

builder.Services.AddScoped<IServiceManager, ServiceManager>();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();


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

app.MapFallbackToFile("index.html");

app.Run();

working controller

using Microsoft.AspNetCore.Mvc;

namespace CloudServiceWebAPI.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
        "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }
        [HttpGet("123")]
        public IEnumerable<WeatherForecast> Get1()
        {
            return Enumerable.Range(1, 2).Select(index => new WeatherForecast
            {


                Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
                TemperatureC = Random.Shared.Next(-20, 55),
                Summary = Summaries[Random.Shared.Next(Summaries.Length)]
            })
            .ToArray();
        }
    }
}

not working controller

using CloudServiceWebAPI.DTOs.AccountController;
using Microsoft.AspNetCore.Mvc;
using POCs.POCs.DTOs;
using POCs.Services.Interfaces;

namespace CloudServiceWebAPI.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class AccountController : ControllerBase
    {
         private readonly ILogger<AccountController> _logger;
        private readonly IServiceManager _serviceManager;

        public AccountController(ILogger<AccountController> logger, IServiceManager serviceManager)
        {
            _logger = logger;
            _serviceManager = serviceManager;
        }

        [HttpGet("123")]
        public GetAccountsResponse GetAccounts()
        {
            GetAccountsResponse response = new GetAccountsResponse();
            response.Success = true;
            response.Message = string.Empty;
            GetAccountsDTO getAccountsDTO = new GetAccountsDTO();
            response.Accounts = this._serviceManager.AccountService.GetAccounts(getAccountsDTO);
            return response;
        }
    }
}

I've tried to change program.cs in many ways, but came a conclusion that it's not the case. Tried to play with names of controllers and with routing policies. Looks like I'm missing something pretty simple. Will be glad for any help


Solution

  • Oh. I've found an answer. Into the latest template for .net 7 webAPI there is a separate script for run redirection from https to http request. And there is a list of allowed routes. file is called "proxy.conf.js".

    Hope my experience will help somebody.