MVC 路由在 Blazor 客户端应用程序中不起作用

MVC Routing not working in Blazor client side application

我试图在我的 Blazor WASM 项目中实现一个简单的 MVC 控制器,但我无法使路由正常工作。当我尝试访问它时,它总是将我重定向到 blazor“NotFound”组件。我花了很多时间尝试在我的 Startup.cs 中进行配置,但我 运行 没有想法。我正在 .NET6 中的样板 WASM 项目上执行此操作。这就是我的 Startup.cs 的样子:

    public void ConfigureServices(IServiceCollection services)
    {

        services.ConfigureApplicationServices();
        services.ConfigurePersistenceServices(Configuration);


        //services.AddMvc();
        services.AddControllersWithViews();
        services.AddRazorPages();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseMigrationsEndPoint();
            app.UseWebAssemblyDebugging();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            // 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.UseMiddleware<ExceptionMiddleware>();


        app.UseCors(config =>
            config
                .AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader()
        //.WithExposedHeaders("header1", "header")
        );

        app.UseHttpsRedirection();
        app.UseBlazorFrameworkFiles();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseIdentityServer();
        app.UseAuthentication();
        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
            endpoints.MapDefaultControllerRoute();
            endpoints.MapRazorPages();
            endpoints.MapFallbackToFile("index.html");
        });
    }

这是我的 MVC 控制器:

[Route("[controller]/[action]")]
public class RoleManagerController : Controller
{
    private readonly RoleManager<IdentityRole> _roleManager;
    public RoleManagerController(RoleManager<IdentityRole> roleManager)
    {
        _roleManager = roleManager;
    }
    public async Task<IActionResult> Index()
    {
        var roles = await _roleManager.Roles.ToListAsync();
        return View(roles);
    }
    [HttpPost]
    public async Task<IActionResult> AddRole(string roleName)
    {
        if (roleName != null)
        {
            await _roleManager.CreateAsync(new IdentityRole(roleName.Trim()));
        }
        return RedirectToAction("Index");
    }
}

[礼貌]你的问题一定是漏了一些信息

我已经使用 Net6.0 开箱即用的 Blazor 托管模板对此进行了测试,它可以正常工作。

这是我的控制器:

[ApiController]
[Route("[controller]/[action]")]
public class MyController : ControllerBase
{

    public string Index()
    {
        return "hello.  You called Index";
    }
}

我的(开箱即用)Program:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseWebAssemblyDebugging();
}
else
{
    app.UseExceptionHandler("/Error");
    // 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.UseBlazorFrameworkFiles();
app.UseStaticFiles();

app.UseRouting();


app.MapRazorPages();
app.MapControllers();
app.MapFallbackToFile("index.html");

app.Run();

而 Postman 结果(使用小写字母):