路线图结构:
routes.MapRoute(
name: "NaturalStonesDetails",
url: "{lang}/natural-stones/{title}-{id}",
defaults: new { lang = "en", controller = "NaturalStones", action = "Details" }
);
routes.MapRoute(
name: "ProductCategorieList",
url: "{lang}/products/{title}-{id}",
defaults: new { lang = "en", controller = "Product", action = "Index" }
);
链接结构:
<a href="@Url.Action("Index", "Product", new { title = stoneguide.com.Models.DealerProduct.GetTitleUrlFormat(items.CategoryName), id = Convert.ToInt32(items.ID) })" style="padding:2px;">
问题:
当我点击链接时,请转到产品页面,该页面应转到NaturalStones页面。我无法解决这个问题。
请帮忙!
答案 0 :(得分:0)
您的路由非常整洁,应该可以正常使用提供的代码。我想你只是对使用哪个控制器感到困惑。所以
@Url.Action("Index", "Product", new { title = "mytitle", id = "myid" })
返回/en/products/mytitle-myid
,路由正确识别为产品控制器的请求,带有两个参数的索引操作。
另一方面
@Url.Action("Details", "NaturalStones", new { title = "mytitle", id = "myid" });
生成/en/natural-stones/mytitle-myid
,它被解释为对NaturalStones的请求,具有两个参数的Details操作,并且可能是您想要使用的那个。
在旁注中,为Product提供title
和id
,Index操作有点尴尬。按照惯例,索引操作通常会返回一个项目列表,因此对特定ID的引用似乎不合适。您可以考虑将路由更改为:
routes.MapRoute(
name: "NaturalStonesDetails",
url: "{lang}/natural-stones/{title}-{id}",
defaults: new { lang = "en", controller = "NaturalStones", action = "Details" }
);
routes.MapRoute(
name: "ProductCategorieList",
url: "{lang}/products",
defaults: new { lang = "en", controller = "Product", action = "Index" }
);
然后控制器如下:
public class ProductController : Controller
{
public ActionResult Index()
{
return View();
}
}
public class NaturalStonesController : Controller
{
public ActionResult Details(string title, string id)
{
return View();
}
}