重定向到另一个控制器+操作而不更改ASP.Net MVC3中的URL

时间:2011-07-10 09:30:13

标签: asp.net-mvc asp.net-mvc-3 asp.net-routing

注意:下面只是一个小型演示,可以模拟我要找的内容:

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

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中提取动物细节+类型

除了现有的网址格式外,我打算以下面的格式创建一个简短的网址

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的问题是他们都改变了网址。但我不想改变我的网址模式。

请建议我如何实现这一点,你可能会提出一些完全不同的方法来实现这个目标

1 个答案:

答案 0 :(得分: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/1时,将执行自定义路由的GetRouteData方法,将获取id = 1,然后它将使用Index的{​​{1}}操作Cat控制器。