ASP .Net MVC Routing: Url only with string ID

后端 未结 2 1877
陌清茗
陌清茗 2021-02-08 10:46

very simple question but I couldn\'t find an answer for this:

I have the default {controller}/{action}/{id} pattern in my global.asax.

I ne

2条回答
  •  醉酒成梦
    2021-02-08 10:57

    I would suggest a separate route with a constraint that the id either can't match one of your controllers or must match one of the ids in the database. List it before the default route so it matches first, if the qualifications are met.

    Example using a simple regex for a fixed constraint, though you'd probably want to create a custom constraint deriving from IRouteConstraint that limits the values dynamically.

    routes.MapRoute(
      "Brands",
      "{id}",
      new { controller = "brand", action = "detail" },
      new { id = "^(Microsoft)|(Apple)$" }
    );
    

    You might want to look at http://stephenwalther.com/blog/archive/2008/08/07/asp-net-mvc-tip-30-create-custom-route-constraints.aspx for more ideas.

提交回复
热议问题