【问题标题】:Redirect to another controller+ action without changing URL in ASP.Net MVC3重定向到另一个控制器操作而不更改 ASP.Net MVC3 中的 URL
【发布时间】:2011-07-10 09:30:13
【问题描述】:

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

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

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

目前的 Controller 是这样的

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 模式。

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

【问题讨论】:

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


    【解决方案1】:

    您可以编写自定义动物路线:

    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,然后它将使用Cat 控制器的Index 操作。

    【讨论】:

    • 我实际上创建了一个 RouteHandler,但是 "rd.Values["controller"]",rd.GetRequiredString --> 修改和获取路由数据的方法很有帮助,我只是在寻找这个.谢谢。
    猜你喜欢
    • 2013-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-24
    • 2014-12-31
    • 1970-01-01
    相关资源
    最近更新 更多