0

I'm trying to implement a simple MVC controller in my Blazor WASM project, but I can't get the routing to work correctly. It always redirects me to the blazor "NotFound" component when I try to access it. I've spent alot of time trying configuring in my Startup.cs but I'm run out of ideas. I'm doing this on a boilerplate WASM project in .NET6. This is how my Startup.cs looks like:

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

And this is my MVC controller:

[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");
    }
}
user1784297
  • 103
  • 12
  • So apparently it worked when I write exactly /RoleManager/Index, but it doesn't work if I write /rolemanager/index. How can I make it work so the routing ignores lowercase / uppercase? – user1784297 Jan 05 '22 at 23:40
  • 1
    Does this answer your question? [How do you enforce lowercase routing in ASP.NET Core?](https://stackoverflow.com/questions/36358751/how-do-you-enforce-lowercase-routing-in-asp-net-core) – Jackdaw Jan 06 '22 at 00:04
  • 1
    I need to confirm with you that whether your application is a Asp.net 6 application, because as we all known, in the .Net 6 application it will use the program.cs file to configure the middleware and service, instead of using the Startup.cs file. Or you creates a .Net 6 application and use both of the Program.cs and Startup.cs file. Then, could you share the relevant code in the program.cs file? – Qing Guo Jan 06 '22 at 08:36
  • 1
    Second, when you create the Blazor WASM project, whether you has checked the Asp.net Core Hosted option (if checked it will generate three project: Client, Server and Shared)? And you want to add controller in the Client project? Generally, in the Blazor WASM project (check the Asp.net Core Hosted option), the Client project contains the Razor components which provide the UI, and the Server project contains the API or MVC controller, which will handle and responds the client request. So, the controller should in the Blazor Server application. – Qing Guo Jan 06 '22 at 08:36

1 Answers1

1

[Polite] There's must be some missing information in your question.

I've tested this using the Net6.0 out-of-the-box Blazor Hosted template and it works.

Here's my controller:

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

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

My (out-of-the-box) 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();

And the Postman result (using small letters):

enter image description here

MrC aka Shaun Curtis
  • 19,075
  • 3
  • 13
  • 31