5

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?

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
Yecats
  • 1,715
  • 5
  • 26
  • 40
  • i recommend you start to go through a beginners tutorial first. I don't even know if you are supposed to be using razor pages or mvc, which are just two different ways of architecting your app. – Johan Herstad Apr 02 '19 at 06:39
  • `MapRoute()` should be used not MapControllerRoute secondly as @JohanHerstad stated that mixing pages with mvc routing can have weird results. – mvermef Apr 02 '19 at 15:51
  • Interesting, I did read several tutorials but I didn't get the feeling it was one or the other. How do you do talk to c# code with just Razer? – Yecats Apr 02 '19 at 17:02
  • I have the same problem after migrate from 2.2 to 3. Did you find the solution? – m4rcel Jun 21 '19 at 12:38
  • I did not - I assumed at this point it was just a bug. – Yecats Jun 22 '19 at 00:06

1 Answers1

2

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");
        });
GGleGrand
  • 1,565
  • 1
  • 20
  • 45