我試圖設置路由如下。只有{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」 –
您需要包含所有路線,因爲訂單很重要。 –