次のようにルーティングを設定しようとしています。 :{id}パラメータのみのルーティングで "リソースが見つかりません"というエラーが返される
私の目標はwww.mysite.com/123
のようなセットアップURLにあるwww.mysite.com/Products/index/123 よう
は、今のところ私のURLが見えます製品は私のコントローラー名、インデックスは私のアクション名で、はがNULL可能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;
}
エラーは、次のURLにスローされます。WWW .mysite.com/123
質問があります: なぜ私のビューはこのエラーを返し、この動作を回避する方法は?
ありがとうございます。
あなたの 'WrappedViewModels'定義はどのように見えますか?これは正常に動作するはずです。あなたに404を与えようとしているURLは何ですか? – Shyju
こんにちは。質問を編集しました。このURLにエラーが発生しました。 "www.mysite.com/123" –
注文が問題であるため、すべてのルートを含める必要があります。 –