Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core 3.0 Controller Routing Not Working

Disclaimer: I'm new to ASP.NET Core / Razor / MVC and am starting out on the 3.0 preview.

What I want to do is have a "button" on my page that adds a new empty item to a list so that the user can input some values. From what I've read (which is quite a bit) it sounds like having a hyperlink point towards a controller is the right approach to this. I cannot get it to actually work though. Here's my code:

Link pointing to controller / action:

<a class="btn btn-success" asp-controller="Customer" asp-action="AddProduct">New Product</a>

Controller:

    public class CustomerController : Controller
{
    public void AddProduct()
    {

        var tmp = "";

    }

    public string Index()
    {
        return "This is my default action...";
    }

    public string Welcome()
    {
        return "This is the Welcome action method...";
    }

}

Startup.cs routing is default:

        app.UseRouting(routes =>
        {
            routes.MapRazorPages();
        });

With this setup, if I click the start button, I see the URL change to the below, but nothing else happens (no break point is hit, for example):

https://localhost:44358/Customers/Create?action=AddProduct&controller=Customer

I have tried to adding the route to specifically to the UseRouting code, like such:

            app.UseRouting(routes =>
        {
            routes.MapRazorPages();
            routes.MapControllerRoute(
                name: "Customer",
                template: "{controller=Customer}/{action=Welcome}");
        });

But when I do this, it seems to break, as the text color changes (from white to black) and nothing happens when I click it.

Any idea on where I'm going wrong?

I do have one more question - which is how do you access the model data from the controller?

like image 318
Yecats Avatar asked Nov 16 '22 12:11

Yecats


1 Answers

See middle of page here: https://devblogs.microsoft.com/aspnet/asp-net-core-updates-in-net-core-3-0-preview-4/

In Startup.cs Configure:

        app.UseRouting();
        app.UseEndpoints(routes =>
        {
            routes.MapRazorPages();
            routes.MapFallbackToPage("/Home");
        });
like image 129
GGleGrand Avatar answered Feb 16 '23 00:02

GGleGrand