0

When using the asp-controller tag helper (https://learn.microsoft.com/en-us/aspnet/core/mvc/views/working-with-forms?view=aspnetcore-3.1#the-form-tag-helper), it should be possible to specify the controller you want to run like this: <form asp-controller="Demo" asp-action="Register" method="post">

Obviously, the controller here is Demo. It seems this is being transmitted from the web browser to the back end using the URL: https://localhost:44311/?action=onPost&controller=TodoItem

But this doesn't work in my case, the controller that runs is not the one in the URL, but the one connected to the page where the form is. How can I debug this, how can I see where things are going wrong? And what is the solution, is there some service to be loaded that enables this behavior?

2 Answers 2

1

It seems this is being transmitted from the web browser to the back end using the URL: https://localhost:44311/?action=onPost&controller=TodoItem

To fix the above issue, you can try to register services used for MVC controllers and add endpoints for controller actions, like below.

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews();
    services.AddRazorPages();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    //...
    //other configuration code here
    //...

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapRazorPages();

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

And make sure your razor project contains controller class(es) that inherit from Microsoft.AspNetCore.Mvc.Controller, like below.

public class DemoController : Controller
{
    //...

Folder structure of my project

enter image description here

Test Result

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Oh so it's likely because I'm using Razor Pages, thanks for your answer! How could I debug this? Is there a log somewhere that says 'controller not found, loading fallback route'?
You can try to set the logging level of Microsoft.AspNetCore.Routing to TRACE, which might log some useful info there.
0

I figured it out - the tag helper asp-controller does nothing when using Razor Pages, instead asp-page should be used, as described here: https://learn.microsoft.com/en-us/aspnet/core/mvc/views/working-with-forms?view=aspnetcore-3.1

To fix this, the docs were helpful, and like @Fei Han commented, trace logging for the routing middleware. So a default appsettings.json will then look like this:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information",
      "Microsoft.AspNetCore.Routing":  "Trace"
    }
  },
  "AllowedHosts": "*"
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.