Asp.net mvc MVC操作具有相同的名称,但控制器不同
路由映射结构:Asp.net mvc MVC操作具有相同的名称,但控制器不同,asp.net-mvc,Asp.net Mvc,路由映射结构: routes.MapRoute( name: "NaturalStonesDetails", url: "{lang}/natural-stones/{title}-{id}", defaults: new { lang = "en", controller = "NaturalStones", action = "Details" } ); routes.MapRoute(
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;">
链接结构:
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页面。我解决不了这个问题,一种
请帮忙 您的路由非常整洁,使用提供的代码应该可以正常工作。我想你只是被该用哪个控制器弄糊涂了。所以
@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的请求,用两个参数详细说明操作,这可能就是您想要使用的参数
另一方面,为产品提供title
和id
,索引操作有点笨拙。按照惯例,索引操作通常返回项目列表,因此对特定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();
}
}