2017-10-28 7 views
0

次のようにルーティングを設定しようとしています。 {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

質問があります: なぜ私のビューはこのエラーを返し、この動作を回避する方法は?

ありがとうございます。

+0

あなたの 'WrappedViewModels'定義はどのように見えますか?これは正常に動作するはずです。あなたに404を与えようとしているURLは何ですか? – Shyju

+0

こんにちは。質問を編集しました。このURLにエラーが発生しました。 "www.mysite.com/123" –

+1

注文が問題であるため、すべてのルートを含める必要があります。 –

答えて

1

In RegisterRoutesでは少し指定する必要があります。

routes.MapRoute(
    name: "OnlyId", 
    url: "{id}", 
    defaults: new { controller = "Products", action = "index" }, 
    constraints: new{ id=".+"}); 

とは、私は、これはあなたがすぐに解決すると思います

@Html.RouteLink("123", routeName: "OnlyId", routeValues: new { controller = "Products", action = "index", id= "id" }) 

として、各アンカータグのルーティングを指定する必要があります。

0

あなたはidパラメータがNULL可能整数値であることを確認していた場合、それは他のルートには影響を与えないように、このような\d正規表現でルート制約を置く:

routes.MapRoute(
    name: "OnlyId", 
    url: "{id}", 
    defaults: new { controller = "Products", action = "index" }, // note that this default doesn't include 'id' parameter 
    constraints: new { id = @"\d+" } 
); 

を使用すると、標準のために満足していない場合パラメータ制約は、あなたがIRouteConstraintを継承するクラスを作成して、この例のようにカスタムルート上のことを適用することができます。

// adapted from /a/11911917/ 
public class CustomRouteConstraint : IRouteConstraint 
{ 
    public CustomRouteConstraint(Regex regex) 
    { 
     this.Regex = regex; 
    } 

    public CustomRouteConstraint(string pattern) : this(new Regex("^(" + pattern + ")$", RegexOptions.CultureInvariant | RegexOptions.Compiled | RegexOptions.IgnoreCase)) 
    { 
    } 

    public Regex Regex { get; set; } 

    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) 
    { 
     if (routeDirection == RouteDirection.IncomingRequest && parameterName == "id") 
     { 
      if (values["id"] == UrlParameter.Optional) 
       return true; 
      if (this.Regex.IsMatch(values["id"].ToString())) 
       return true; 

      // checking if 'id' parameter is exactly valid integer 
      int id; 
      if (int.TryParse(values["id"].ToString(), out id)) 
       return true; 
     } 
     return false; 
    } 
} 

はその後にカスタムルート制約を置きます210ベースのルートは、他のルートを動作させる:

routes.MapRoute(
    name: "OnlyId", 
    url: "{id}", 
    defaults: new { controller = "Products", action = "index", id = UrlParameter.Optional }, 
    constraints: new CustomRouteConstraint(@"\d*") 
); 
0

あなたはルーティングの順序を逃したと思います。したがって、使用可能なすべてのコントローラを処理し、残りの要求、たとえばwww.mysite.com/{id}種類の要求を処理する要求を処理するものを定義する最初のルート定義を作成します。

のでスワップOnlyIdデフォルト規則や必要なこれ以上の変更。今はうまくいくはずだと思う。

関連する問題