0

I am trying to learn ASP.NET MVC, and a project I am trying to do is to create a website with a few books. When you add a few books of the same series, it should give a discount but does not apply to any book of another series in a Shopping cart (for example 2 Marvel comics would give 10% off those but not any discount on a DC comic).

However the issue I am having is creating a shopping cart in the first place, I was following the following tutorial on this:

https://learningprogramming.net/net/asp-net-core-mvc/build-shopping-cart-with-session-in-asp-net-core-mvc/

But it seems out of date and I get an error of:

AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied:

BookStore.Controllers.ProductController.Index (BookStore) Page: /Index

And I am not sure why, I'm new to routing so I can assume I've missed something, I'll include the code below:

namespace BookStore.Controllers
{
    [Route("product")]
    public class ProductController : Controller
    {
        [Route("")]
        [Route("index")]
        [Route("~/")]
        public IActionResult Index()
        {
            ProductModel productModel = new ProductModel();
            ViewBag.products = productModel.findAll();
            return View();
        }
    }
}

namespace BookStore.Controllers
{
    [Route("cart")]
    public class CartController : Controller
    {
        [Route("index")]
        public IActionResult Index()
        {
            var cart = SessionHelper.GetObjectFromJson<List<Item>>(HttpContext.Session, "cart");
            ViewBag.cart = cart;
            ViewBag.total = cart.Sum(item => item.Product.Price * item.Quantity);
            return View();
        }

        [Route("buy/{id}")]
        public IActionResult Buy(string id)
        {
            ProductModel productModel = new ProductModel();
            if (SessionHelper.GetObjectFromJson<List<Item>>(HttpContext.Session, "cart") == null)
            {
                List<Item> cart = new List<Item>();
                cart.Add(new Item { Product = productModel.find(id), Quantity = 1 });
                SessionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart);
            }
            else
            {
                List<Item> cart = SessionHelper.GetObjectFromJson<List<Item>>(HttpContext.Session, "cart");
                int index = isExist(id);
                if (index != -1)
                {
                    cart[index].Quantity++;
                }
                else
                {
                    cart.Add(new Item { Product = productModel.find(id), Quantity = 1 });
                }
                SessionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart);
            }
            return RedirectToAction("Index");
        }
    }
}

And I have been taking a look at this one as well

https://learn.microsoft.com/en-us/aspnet/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/create_the_data_access_layer

As it is using the latest version

Rahul Sharma
  • 7,768
  • 2
  • 28
  • 54
SCB3603
  • 35
  • 7

1 Answers1

1

You can add the Name property to your HTTP verb attribute for your Controller method-Index. In your case, you can do this:

//ProductController Index method
[Route("")]
[Route("index")]
[Route("~/")]
[HttpGet(Name = "Get my products")]
public IActionResult Index()

//CartController Index method
[Route("index")]
[HttpGet(Name = "Get my cart data")]
public IActionResult Index()

You can read more why you can't have two paths with same name on different controllers here

You also need to setup your routes correctly:

app.UseMvc(routes =>  
{  
    routes.MapRoute(
      name: "ByProduct",
      template: "{controller}/{action}/{id?}",
      defaults: new { controller = "Product", action = "Index"});  

  routes.MapRoute(  
      name: "default",  
      template: "{controller=Home}/{action=Index}/{id?}");  
});
Rahul Sharma
  • 7,768
  • 2
  • 28
  • 54
  • Thanks, I have a page loading now, the Normal Default page, but from I can see it should be loading the new one: app.UseMvc(routes => { routes.MapRoute( name: "Get my products", template: "{controller=ProductController}/{action=Index}/{id?}"); }); – SCB3603 Jan 30 '22 at 16:04
  • @SCB3603 What do you mean new one? What is the expected behavior that you require? – Rahul Sharma Jan 30 '22 at 16:08
  • Even when I change the Route, the Homepage remains the same as before, but its now loading – SCB3603 Jan 30 '22 at 16:29
  • @SCB3603 Shouldn't the route be: `app.UseMvc(routes => { routes.MapRoute( name: "Get my products", template: "{controller=Product}/{action=Index}/{id?}"); });` – Rahul Sharma Jan 30 '22 at 16:32
  • Nope, it doesn't load that page either way – SCB3603 Jan 30 '22 at 17:01
  • @SCB3603 Can you please post how you have configured your routes in `Startup.cs`? – Rahul Sharma Jan 30 '22 at 17:02
  • ` app.UseMvc(routes => { routes.MapRoute( name: "Get my products", template: "{controller=Product}/{action=Index}/{id?}"); }); ` – SCB3603 Jan 30 '22 at 17:16
  • @SCB3603 Which version of .NET Core are you using? Also in your routing configuration, have you setup a default route (this route will come in the last). The problem is your routes are not defined correctly. You can refer to this doc based on your .NET Core version: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/routing?view=aspnetcore-5.0 – Rahul Sharma Jan 30 '22 at 17:24
  • Using .net 5.0, and I think so? Where would I find that? – SCB3603 Jan 30 '22 at 17:27
  • @SCB3603 To check which .NET Core Version is installed you can run the following command on the command prompt/terminal in V.S: `dotnet --version` – Rahul Sharma Jan 30 '22 at 17:29
  • Yep, 5.0, I also added these 2 lines into Startup.cs app.UseMvc(); app.UseMvcWithDefaultRoute(); – SCB3603 Jan 30 '22 at 17:35
  • @SCB3603 Updated answer to add route configuration also – Rahul Sharma Jan 30 '22 at 17:47
  • Thank you!, It does seem to be working in that regard, not displaying what I want, but I can see its going to the right page – SCB3603 Jan 30 '22 at 18:31