Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix a 404 with routes in ASP.NET MVC?

I'm having a problem trying to get routing to work with ASP.NET MVC 3.0. I have the following routes declared:

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

    routes.MapRoute(
        "Default",
        "{controller}/{action}/{id}",
    new { controller = "Home", action = "RsvpForm", id = UrlParameter.Optional } 
    );

    routes.MapRoute(
        "TestRoute",
        "{id}",
        new { controller = "Product", action = "Index3", id = UrlParameter.Optional }
    );

    routes.MapRoute(
        "TestRoute2",
        "{action}",
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

When I visit:

http://localhost

The site works correctly, and it appears to hit Default route.

When I visit:

http://localhost/1

I get a 404:

Server Error in '/' Application.

The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /1

Here are the actions those routes correspond to:

public ActionResult Index3(int? id)
{
    Product myProduct = new Product
    {
        ProductID = 1,
        Name = "Product 1 - Index 3",
        Description = "A boat for one person",
        Category = "Watersports",
        Price = 275M
    };

    Product myProduct2 = new Product
    {
        ProductID = 2,
        Name = "Product 2 - Index 3",
        Description = "A boat for one person",
        Category = "Watersports",
        Price = 275M
    };

    ViewBag.ProcessingTime = DateTime.Now.ToShortTimeString();

    if (id == 1)
        return View("index", myProduct);
    else
        return View("index", myProduct2);
}

How do I structure my routes so that all three action methods are hit correctly?

like image 717
Stiger Avatar asked Jan 05 '12 04:01

Stiger


People also ask

How can add route in ASP.NET MVC?

Configure a Route Every MVC application must configure (register) at least one route configured by the MVC framework by default. You can register a route in RouteConfig class, which is in RouteConfig. cs under App_Start folder. The following figure illustrates how to configure a route in the RouteConfig class .

Can we have multiple routes in MVC?

Creating Custom Routes in ASP.NET MVC Application: The Default parameter is optional. The point that you need to remember is, the Route Names must be unique. You can register multiple custom routes with different names.

What is route constraint in MVC?

The Route Constraint in ASP.NET MVC Routing allows us to apply a regular expression to a URL segment to restrict whether the route will match the request. In simple words, we can say that the Route constraint is a way to put some validation around the defined route.


2 Answers

ASP.NET MVC Routing evaluates routes from top to bottom. So if two routes match, the first one it hits (the one closer to the 'top' of the RegisterRoutes method) will take precedence over the subsequent one.

With that in mind, you need to do two things to fix your problem:

  1. Your default route should be at the bottom.
  2. Your routes need to have constraints on them if they contain the same number of segments:

What's the difference between:

example.com/1

and

example.com/index

To the parser, they contain the same number of segments, and there's no differentiator, so it's going to hit the first route in the list that matches.

To fix that, you should make sure the routes that use ProductIds take constraints:

routes.MapRoute(
    "TestRoute",
    "{id}",
    new { controller = "Product", action = "Index3", id = UrlParameter.Optional },
    new { id = @"\d+" } //one or more digits only, no alphabetical characters
);

There are other issues with your set up, but those are two things that come to mind right off the bat.

like image 124
George Stocker Avatar answered Sep 30 '22 14:09

George Stocker


Your routes MapRoute Default should be the last.

routes.MapRoute(
        "Default",
        "{controller}/{action}/{id}",
    new { controller = "Home", action = "RsvpForm", id = UrlParameter.Optional } 
    );
like image 45
Asuquo12 Avatar answered Sep 30 '22 12:09

Asuquo12