6

注意:下面只是一个小的演示排序来模拟我正在寻找的内容:

以下是我的应用程序上用户可以看到的网址格式

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 回答