Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何创建www.domain.com/product MVC路由_C#_Asp.net_Asp.net Mvc_Asp.net Mvc Routing - Fatal编程技术网

C# 如何创建www.domain.com/product MVC路由

C# 如何创建www.domain.com/product MVC路由,c#,asp.net,asp.net-mvc,asp.net-mvc-routing,C#,Asp.net,Asp.net Mvc,Asp.net Mvc Routing,我正在尝试创建一个http://www.domain.com/product路线。 它应该在数据库中查找产品名称,如果找到,则调用控制器,如果没有,则转到下一个路由 我已经尝试创建下面的路由,但是如果在数据库中找不到{shortcut}产品名称,我无法确定如何遵循下一个路由 routes.MapRoute( name: "easyshortcut", url: "{shortcut}", defaults: new { controller = "Home", action = "P

我正在尝试创建一个
http://www.domain.com/product
路线。 它应该在数据库中查找产品名称,如果找到,则调用控制器,如果没有,则转到下一个路由

我已经尝试创建下面的路由,但是如果在数据库中找不到
{shortcut}
产品名称,我无法确定如何遵循下一个路由

routes.MapRoute(
  name: "easyshortcut",
  url: "{shortcut}",
  defaults: new { controller = "Home", action = "Product" }
);

谢谢

您可以通过路由约束来执行此操作:

routes.MapRoute(
    name: "easyshortcut",
    url: "{shortcut}",
    defaults: new { controller = "Home", action = "Product" },
    constraints: new { name = new ProductMustExistConstraint() }
);
public class ProductMustExistConstraint : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext, 
        Route route, 
        string parameterName, 
        RouteValueDictionary values, 
        RouteDirection routeDirection)
    {
        var productNameParam = values[parameterName];
        if (productNameParam != null)
        {
            var productName = productNameParam.ToString();

            /* Assuming you use Entity Framework and have a set of products 
             * (you can replace with your own logic to fetch the products from 
             *  the database). 
             */

            return context.Products.Any(p => p.Name == productName);
        }

        return false;

    }
}
其中
name
HomeController
的产品操作中的参数名称

然后实现约束:

routes.MapRoute(
    name: "easyshortcut",
    url: "{shortcut}",
    defaults: new { controller = "Home", action = "Product" },
    constraints: new { name = new ProductMustExistConstraint() }
);
public class ProductMustExistConstraint : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext, 
        Route route, 
        string parameterName, 
        RouteValueDictionary values, 
        RouteDirection routeDirection)
    {
        var productNameParam = values[parameterName];
        if (productNameParam != null)
        {
            var productName = productNameParam.ToString();

            /* Assuming you use Entity Framework and have a set of products 
             * (you can replace with your own logic to fetch the products from 
             *  the database). 
             */

            return context.Products.Any(p => p.Name == productName);
        }

        return false;

    }
}
(以上内容已根据此情况进行了调整。)