user1784297
user1784297

Reputation: 105

MVC Routing not working in Blazor client side application

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

Upvotes: 0

Views: 1995

Answers (1)

MrC aka Shaun Curtis
MrC aka Shaun Curtis

Reputation: 30046

[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

Upvotes: 1

Related Questions