问题描述:

If an action exists on a controller, does asp.net-mvc route to that action before attempting to process any custom mapped routes?

Example.

Say I have the following controller

public class ShopController : Controller

{

public ActionResult Shop(Category category)

{

// returns some result

}

public ActionResult CartItemCount()

{

// returns some result

}

}

And I have registered the following route in my route collection:

public static void RegisterRoutes(RouteCollection routes)

{

routes.MapRoute(

"Shop",

"Shop/{category}",

new { controller = "Shop", action = "Shop", category = UrlParameter.Optional } // Parameter defaults

);

}

What would happen if I had the following URL?

http://www.example.com/Shop/CartItemCount

Edit

I confused myself by thinking that the name of the mapped route was related to how it was processed. That is not the case, when it comes to url matching the name of the route does not matter.

As it turns out I had another route defined just above the one I gave in the example. This route, though named differently, was getting matched. I didn't even think to check it because, as I said, I thought the name given to a route somehow impacted the matching.

网友答案:

The routes decide which action to use. If there are no routes defined, you wont hit an action even if it exists. Install RouteDebugger and fire off your url. It will tell you ALL routes that it matches and which one it has actually used.

相关阅读:
Top