默认的MVC路线几乎已经完成了。
看那控制器=和行动=
因此,让一个网址,如:
/店/ {ID}
控制器= ShoppingController,行动=店
注意我加假设用户不必指定产品,并且您得到一些字符串来告诉您这是默认产品,您也可以使用UrlParameter.Optional,并且您将为ID为
你的控制器看起来像:
public class ShoppingController : Controller
{
public ActionResult Shop(string id)
{
if (string.IsNullOrEmpty(id) || string.Equals(id, "DefaultProduct", StringComparison.OrdinalIgnoreCase))
{
// Do something to please the user
}
// Get product by id
return View();
}
}
和路由代码:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Shopping",
url: "shop/{id}",
defaults: new { controller = "Shopping", action = "Shop" , id = "DefaultProduct" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}