ASP.NET MVC Routing - Unable to Map Custom Route

Charlotte Garcia 20 Reputation points
2024-02-01T05:43:14.05+00:00

Hello community, I'm currently working on an ASP.NET MVC project for my personal website Parenting Wall and facing challenges with custom routing. I've defined a custom route in my RouteConfig.cs file, but it doesn't seem to be working as expected. Here's the route definition:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        // Custom route
        routes.MapRoute(
            name: "CustomRoute",
            url: "products/{category}/{id}",
            defaults: new { controller = "Products", action = "Details", id = UrlParameter.Optional }
        );

        // Other routes...
    }
}

I expect URLs like /products/baby-product/52412 to be handled by the Details action in the Products controller. However, when I navigate to such URLs, I get a 404 page not found error. I've double-checked the controller and action names, and they seem correct. Could someone please guide me on troubleshooting custom routes in ASP.NET MVC? Are there common pitfalls or additional steps I might be missing? Any help or suggestions would be greatly appreciated. Thank you!

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,146 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,165 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 22,751 Reputation points Microsoft Vendor
    2024-02-01T06:59:05.28+00:00

    Hi @Charlotte Garcia,

    How is the code in your "Details" action written?

    If you want to return a view, you need to createDetails.cshtml.

    Below is my test for your reference.

     public static void RegisterRoutes(RouteCollection routes)
     {
         routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
         // Custom route
         routes.MapRoute(
             name: "CustomRoute",
             url: "products/{category}/{id}",
             defaults: new { controller = "Products", action = "Details", id = UrlParameter.Optional }
         );
    
     }
    

    Controller

    public class ProductsController : Controller
    {       
        Test1 test1 = new Test1();
        public ActionResult Details(string category,int id)
        {
            test1.Id = id;
            test1.category= category;
            return View(test1);
        }
    }
    

    Cshtml

    @model MvcRazor.Models.Test1
    
    @{
        ViewBag.Title = "Details";
    }
    
    <h2>Details</h2>
    
    <div>
        <h4>Test1</h4>
        <hr />
        <dl class="dl-horizontal">
            <dt>
                @Html.DisplayNameFor(model => model.category)
            </dt>
    
            <dd>
                @Html.DisplayFor(model => model.category)
            </dd>
    
        </dl>
    </div>
    <p>
        @Html.ActionLink("Edit", "Edit", new { id = Model.Id }) |
        @Html.ActionLink("Back to List", "Index")
    </p>
    
    public class Test1
    {
        public int Id { get; set; }
        public string category { get; set; }
    }
    

    User's image

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful