我试图设置路由如下。只有{id}参数的路由返回“无法找到资源”错误
现在我的URL看起来像www.mysite.com/Products/index/123
我的目标是建立URL像www.mysite.com/123
其中:产品是我的控制器名称,索引是我的动作名称和是可以为空id
参数。
这是我的路线:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"OnlyId",
"{id}",
new { controller = "Products", action = "index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
,这是我的操作方法
public ActionResult index (WrappedViewModels model,int? id)
{
model.ProductViewModel = db.ProductViewModels.Find(id);
model.ProductImagesViewModels = db.ProductImagesViewModels.ToList();
if (id == null)
{
return HttpNotFound();
}
return View(model);
}
这是我的模型的包装:
public class WrappedViewModels
{
public ProductViewModel ProductViewModel { get; set; }
public ProductImagesViewModel ProductImagesViewModel { get; set; }
public List<ProductImagesViewModel> ProductImagesViewModels { get; set;
}
则抛出Error这个网址:WWW .mysite.com/123
现在的问题是: 为什么我的视图返回这个错误以及如何避免这种行为?
在此先感谢。
你的'WrappedViewModels'定义是怎样的?这应该很好。你正在尝试给你404的网址是什么? – Shyju
嗨。我编辑了我的问题,在此URL上发生错误“www.mysite.com/123” –
您需要包含所有路线,因为订单很重要。 –