首先,我需要说我在项目中使用T4MVC。我有一种方法用于两条路线:
public virtual ActionResult List(string slug, string category, String selectedFilters)
路线:
routes.MapRoute("ProductOnSale", "products/{slug}/{category}/onsale", MVC.Product.List());
routes.MapRoute("ProudctsList", "products/{slug}/{category}/{selectedFilters}", MVC.Product.List()
.AddRouteValue("selectedFilters", ""));
正如您所看到的,这只是两条路线的一个ActionResult
。他们有一个不同的url。第一条路线示例:
products/living-room-furniture/sofas/sectional-sofa
第二条路线示例:
products/living-room-furniture/living-room-tables/onsale
这篇文章应该说我来自另一页。我需要将布尔参数添加到我的方法List(string slug, string category, String selectedFilters, bool onsale)
中,并根据情况选择路由。是否可以使用约束?有人能举一个例子说明在这种情况下如何做到这一点吗?
我不确定我是否正确理解你的问题。我来过的两个箱子可能对你有帮助。
情况1:根据用于访问页面的URL重定向到另一个URL。
步骤1:创建MVCRouteHandler
公共类LandingPageRouteHandler:MvcRouteHandler{受保护的覆盖IHttpHandler GetHttpHandler(RequestContext Context){if(Context.HttpContext.Request.Url.DnsSafeHost.ToLower().Contains("abc")){Context.RouteData.Values["controller"]="LandingPage";Context.RouteData.Values["action"]="Index";Context.RouteData.Values["id"]="abc";}返回基地。GetHttpHandler(上下文);}}
步骤2:添加RouteHandler
routes.MapRoute(
name: "Landingpage",
url: "Landingpage/{id}/{*dummy}",
defaults: new { controller = "Landingpage", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
).RouteHandler = new LandingPageRouteHandler();
案例2:根据使用的url向控制器和视图添加属性
在我的例子中,所有的控制器都派生自BaseController类。在BaseController中,我有:
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
//Set TenantByDomain
var DnsSafeHost = filterContext.HttpContext.Request.Url.DnsSafeHost;
int? TenantByDomain = null;
if (db.Tenants.Any(x => x.DnsDomains.Contains(DnsSafeHost)))
{
TenantByDomain = db.Tenants.First(x => x.DnsDomains.Contains(DnsSafeHost)).Id;
}
((BaseController)(filterContext.Controller)).TenantByDomain = TenantByDomain;
filterContext.Controller.ViewBag.TenantByDomain = TenantByDomain;
}
应用于您的问题。使用routehandler,您可以添加一个额外的属性来指示所采用的原始路由,并将两者重定向到第三个url(!用户看不到这个新url)。
在OnActionExecuting中,用额外的routevalue做一些事情,以便可以根据需要进行处理。