[英]Select route depends on URL parameter. Constraints
First of all, I need to say that I'm using T4MVC in my project. 首先,我需要说的是我在项目中使用的是T4MVC。 I have one method for two routes : 我有两条路线的一种方法:
public virtual ActionResult List(string slug, string category, String selectedFilters)
Routes: 路线:
routes.MapRoute("ProductOnSale", "products/{slug}/{category}/onsale", MVC.Product.List());
routes.MapRoute("ProudctsList", "products/{slug}/{category}/{selectedFilters}", MVC.Product.List()
.AddRouteValue("selectedFilters", ""));
As you can see, this is only one ActionResult
for two routes. 如您所见,这只是两条路由的一个ActionResult
。 They have a different url. 它们具有不同的URL。 Example for the first route: 第一条路线的示例:
products/living-room-furniture/sofas/sectional-sofa
Example for the second route: 第二条路线的示例:
products/living-room-furniture/living-room-tables/onsale
This piece should say that I came from the another page. 这句话应该说我来自另一页。 I need to add Boolean parameter to my method List(string slug, string category, String selectedFilters, bool onsale)
and, depends on that, choose route. 我需要将布尔参数添加到我的方法List(string slug, string category, String selectedFilters, bool onsale)
并List(string slug, string category, String selectedFilters, bool onsale)
选择路由。 Is it possible to do using constraints? 可以使用约束吗? May anyone provide an example how to do it in this case? 在这种情况下,任何人都可以提供示例示例吗?
I'm not sure if I understand your question correctly. 我不确定我是否正确理解您的问题。 Two cases I come accross might help you. 我遇到的两种情况可能会对您有所帮助。
CASE 1 : redirect to another URL depending on the URL used to access the page. 情况1:根据用于访问页面的URL重定向到另一个URL。
STEP 1: Create an MVCRouteHandler 步骤1:建立MVCRouteHandler
public class LandingPageRouteHandler : MvcRouteHandler { protected override IHttpHandler GetHttpHandler(RequestContext Context) { if ( Context.HttpContext.Request.Url.DnsSafeHost.ToLower().Contains("abc")) { Context.RouteData.Values["controller"] = "LandingPage"; 公共类LandingPageRouteHandler:MvcRouteHandler {受保护的重写IHttpHandler GetHttpHandler(RequestContext Context){if(Context.HttpContext.Request.Url.DnsSafeHost.ToLower()。Contains(“ abc”)){Context.RouteData.Values [“ controller”] = “登陆页面”; Context.RouteData.Values["action"] = "Index"; Context.RouteData.Values [“ action”] =“索引”; Context.RouteData.Values["id"] = "abc"; Context.RouteData.Values [“ id”] =“ abc”; } return base.GetHttpHandler(Context); 返回base.GetHttpHandler(Context); } } }}
STEP 2: Add a RouteHandler 步骤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();
CASE 2 : Add a property to a controller and view depending on the url used 案例2:将属性添加到控制器并根据所使用的URL进行查看
All controlles in my case derive from a BaseController class. 在我的情况下,所有控件都来自BaseController类。 In the BaseController I have : 在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;
}
Applied to your Question. 应用于您的问题。 Using the routehandler you could add an extra property indicating the original route taken and redirect both to a 3th url (! the user does not see this new url). 使用routehandler,您可以添加一个额外的属性来指示原始路线,并将两者都重定向到第3个网址(!用户看不到该新网址)。
In the OnActionExecuting do something with the extra routevalue so that the handling can be done as desired. 在OnActionExecuting中,使用多余的routevalue做一些事情,以便可以根据需要进行处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.