我试图像这样实现slug url
www.abcd.com/xyz/er/productname
其中“/ xyx / er”是区域并且它正在工作,但是当我试图去另一个页面时
www.abcd.com/xyz/er/Login
然后它只是保持重定向。
这是我的RegisterArea代码:
context.MapRoute(
name: "product",
url: "xyz/er/{*slug}",
defaults: new { controller = "er", action = "Index", slug = UrlParameter.Optional }
);
context.MapRoute(
"default",
"xyz/{controller}/{action}/{id}",
new { controller="er", action = "Index", id = UrlParameter.Optional }
);
我的控制器代码是:
public ActionResult Index(string slug=null)
{
if (slug != null && (slug != "Index" && slug != "index"))
{
//it will show the product
}
else
{
//redirect to another page
}
}
答案 0 :(得分:0)
在斯蒂芬的评论的帮助下,我实施了一个约束来检查我的产品名称。如果产品名称有效,它将显示目标页面,如果没有找到,则会重定向到登录页面。
代码如下:
context.MapRoute(
name: "product",
url: "xyz/er/{*slug}",
defaults: new { controller = "er", action = "Index", slug = UrlParameter.Optional },
constraints: new { productname= new ProductNameConstraint() }
);
public class ProductNameConstraint: IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
//logic here
}
}