6

:以下は、私が探しているものをシミュレートするための小さなデモソートです:

以下は、ユーザーが見ることができる私のアプリの URL 形式です

mydomain.com/cat/1  --display cat with id 1 |controller=Cat, action=DisplayDetails
mydomain.com/dog/2  --display dog with id 2 |controller=Dog, action=DisplayDetails
mydomain.com/cow/2 --display cow with id 3  |controller=Cow, action=DisplayDetails

私は 2 匹の動物 (種類が異なる可能性があります) が同じ ID を持つことができないシステムを維持してきました。つまり、id=1 の猫がいる場合、その ID を持つ他の動物を飼うことはできません。また、私のシステムから、動物のIDから動物の詳細+タイプを抽出できます

既存の URL パターンとは別に、次のような形式の短い URL を作成する予定です。

mydomain.com/1  --this will show cat
mydomain.com/2  --this will show dog
mydomain.com/3  --this will show cow

私が作成したルートは以下の通りで、global.asax では同じ順序で表示されます

pattern= Cat/{id}, controller= Cat, action=DisplayDetails
pattern= Dog/{id}, controller= Dog, action=DisplayDetails
pattern= Cow/{id}, controller= Cow, action=DisplayDetails
pattern= {id}, controller= DisplayAnyAnimal ----------i want help in this Route

現在、コントローラーは次のようになっています

public class DisplayAnyAnimalContoller : Controller
{
      public ActionResult Index(string animalId)
      {
           //iam processing request here from animalId
           //now i know which contoller+action needs to be executed

          //say for instant i have to display dog with id=2

          //currently iam doing this to redirect and its working fine, 
          //but it changes url
          -----------------------------------------------
          #########################
          ### i need help here  ###       
          #########################
         return RedirectToRoute(new {contoller="Dog",action="DisplayDetails",id=2 });             
          -----------------------------------------------
      }
}

RedirectToRoute/の問題RedirectToActionは、どちらも URL を変更することです。しかし、URLパターンを変更したくありません。

これを達成する方法を教えてください。これを達成するために、まったく別の方法を提案するかもしれません

4

1 に答える 1

6

カスタム動物ルートを書くことができます:

public class AnimalRoute : Route
{
    public AnimalRoute(string url, IRouteHandler routeHandler)
        : base(url, routeHandler)
    { }

    public override RouteData GetRouteData(HttpContextBase httpContext)
    {
        var rd = base.GetRouteData(httpContext);
        var id = rd.GetRequiredString("id");

        // TODO: Given the id decide which controller to choose:
        // you could query the database or whatever it is needed.
        // Basically that will be the code you had in the beginning
        // of the index action of your DisplayAnyAnimalContoller which
        // is no longer needed.
        if (id == "1")
        {
            rd.Values["controller"] = "Cat";
        }
        else if (id == "2")
        {
            rd.Values["controller"] = "Dog";
        }
        else if (id == "3")
        {
            rd.Values["controller"] = "Cow";
        }
        else
        {
            // no idea what this animal was
            throw new HttpException(404, "Not found");
        }
        rd.Values["action"] = "index";
        return rd;
    }
}

そしてそれをに登録しGlobal.asaxます:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.Add(new AnimalRoute("{id}", new MvcRouteHandler()));
}

に移動すると、カスタム ルートmydomain.com/1GetRouteDataメソッドが実行され、id=1 が取得され、コントローラーのIndexアクションが使用されます。Cat

于 2011-07-10T10:21:28.583 に答える