Search code examples
c#asp.net-coreasp.net-core-mvc

Routing in ASP.NET Core MVC with controllers and views


I'm still trying to explore the routing but I couldn't get the job done. I get confused on how do I use the views and controllers.

I am trying to achieve URL like

http://localhost:5036/Dashboard/Products

because I'm creating a CRUD app but trying in different routes, but it is popping up an error and the only thing I can think of is that about the file path or the controllers.

I have an sample image for which the products is inside the dashboard.

Dashboard

First I have the _DashboardLayout.cshtml

...
<ul class="navbar-nav flex-grow-1">
    <li class="nav-item">
        <a class="nav-link text-dark" asp-area="" asp-controller="Dashboard" asp-action="Index">
            Dashboard</a>
    </li>
    <li class="nav-item">
        <a class="nav-link text-dark" asp-area="" asp-controller="Products"
            asp-action="Index">Products</a>
    </li>
    <li class="nav-item">
        <a class="nav-link text-dark" asp-area="" asp-controller="Products"
            asp-action="Create">Create Product</a>
    </li>
</ul>
...

And the Controller/ProductsController.cs:

using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Crud.Entities;
using Crud.Models;
using System.Threading.Tasks;

namespace Crud.Controllers.Dashboard
{
    // [Route("Dashboard/[controller]")]
    public class ProductsController : Controller
    {
        private readonly AppDbContext _context;

        public ProductsController(AppDbContext context)
        {
            _context = context;
        }

        // GET: Dashboard/Products
        [HttpGet]
        public async Task<IActionResult> Index()
        {
            var products = await _context.Products.ToListAsync();
            return View("Dashboard/Products/Index", products);
        }

        // GET: Dashboard/Products/Create
        [HttpGet("Create")]
        public IActionResult Create()
        {
            return View("Dashboard/Products/Create");
        }

        // POST: Dashboard/Products/Create
        [HttpPost("Create")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Id,ProductName,ProductBrand, Quantity, TypeOfProduct, Price, AgeRange")] Product product)
        {
            if (ModelState.IsValid)
            {
                _context.Add(product);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }

            return View("Dashboard/Products/Create", product);
        }

        // GET: Dashboard/Products/Edit/5
        [HttpGet("Edit/{id:int}")]
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null)  
                return NotFound();

            var product = await _context.Products.FindAsync(id);

            if (product == null) 
                return NotFound();

            return View("Dashboard/Products/Edit", product);
        }

        // POST: Dashboard/Products/Edit/5
        [HttpPost("Edit/{id:int}")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, [Bind("Id,ProductName,ProductBrand, Quantity, TypeOfProduct, Price, AgeRange")] Product product)
        {
            if (id != product.Id) 
                return NotFound();

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(product);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ProductExists(product.Id)) 
                        return NotFound();
                    throw;
                }

                return RedirectToAction(nameof(Index));
            }

            return View("Dashboard/Products/Edit", product);
        }

        // GET: Dashboard/Products/Delete/5
        [HttpGet("Delete/{id:int}")]
        public async Task<IActionResult> Delete(int? id)
        {
            if (id == null) 
                return NotFound();

            var product = await _context.Products.FindAsync(id);

            if (product == null) 
                return NotFound();

            return View("Dashboard/Products/Delete", product);
        }

        // POST: Dashboard/Products/Delete/5
        [HttpPost("Delete/{id:int}")]
        [ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(int id)
        {
            var product = await _context.Products.FindAsync(id);
            _context.Products.Remove(product);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

        private bool ProductExists(int id)
        {
            return _context.Products.Any(e => e.Id == id);
        }
    }
}

And the DashboardController.cs:

using Microsoft.AspNetCore.Mvc;

using System.Diagnostics;
using Crud.Models;

namespace Crud.Controllers
{
    public class DashboardController : Controller
    {
        public IActionResult Index()
        {
            // Add any additional logic for the dashboard here
            return View(); // Return the dashboard view
        }


        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}

And this are the error I keep receiving. After clicking in the _DashboardLayout.cshtml links

Error 1

Error 2


Solution

  • you can set exact location of your view with

    return View("~/Views/yourPath.cshtml")
    

    or you can read this link