简体   繁体   English

选择路由取决于URL参数。 约束条件

[英]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.

相关问题 查询参数路由约束 - Query parameter route constraints 在URL中使用斜杠“/”路由参数 - Route parameter with slash “/” in URL MVC路由URL不包含参数 - MVC route URL not containing parameter 链接参数操作无效。 水晶报告 - Operation illegal on linked parameter. crystal report 如果使用路由约束时路由参数验证失败,为什么 API 返回 404 而不是 400 - Why does the API return a 404 instead of a 400 if a route parameter validation fails when using route constraints 取决于MSF中的决策的参数 - Parameter that depends on a Decision in MSF 以HttpResponse对象作为参数的方法的单元测试。 OutputStream不可用 - Unit test for a method that takes a HttpResponse object as a parameter. OutputStream is not available 通用参数。 如何传入构造函数然后添加到列表? - Generic Parameter. How to pass in to a constructor and then add to a list? NHibernate如何实现功能的分配 <T> 参数。 - How does NHibernate achieve assignment of a Func<T> parameter. C#位图:System.ArgumentException“参数无效”。 - C# Bitmap: System.ArgumentException “Invalid parameter.”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM