2011-07-10 31 views
6

Note: Dưới đây chỉ là một bản demo loại nhỏ để mô phỏng những gì tôi đang tìm kiếm:Chuyển tới một bộ điều khiển + hành động mà không thay đổi URL trong ASP.Net MVC3

Dưới đây là các định dạng url trên ứng dụng của tôi mà người dùng có thể thấy

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 

tôi đã duy trì một hệ thống mà không có 2 con vật (có thể của loại hình khác nhau) có thể có cùng id, nó có nghĩa là nếu có một con mèo với id = 1, chúng ta không thể có bất kỳ động vật khác với id đó . Cũng từ hệ thống của tôi tôi có thể trích xuất các chi tiết động vật + gõ chỉ từ id vật

Ngoài mẫu URL hiện tại, tôi đang lên kế hoạch để tạo ra một Url ngắn ở định dạng như dưới đây

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

đường tôi đã tạo ra như dưới đây , và chúng xuất hiện cùng một trật tự trong 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 

Hiện nay điều khiển trông như thế này

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 });    
      ----------------------------------------------- 
     } 
} 

Bây giờ, vấn đề với RedirectToRoute/RedirectToAction là cả hai đều thay đổi URL. Nhưng tôi không muốn thay đổi mẫu url của mình.

Xin gợi ý cho tôi làm thế nào để đạt được điều này, bạn có thể gợi ý một số cách hoàn toàn khác nhau, để đạt được điều này

Trả lời

6

Bạn có thể viết một lộ trình tùy chỉnh động vật:

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; 
    } 
} 

và sau đó đăng ký nó trong Global.asax:

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

Bây giờ khi bạn điều hướng đến mydomain.com/1, phương pháp GetRouteData của tuyến tùy chỉnh sẽ là exec uted, sẽ tìm nạp id = 1, và sau đó nó sẽ sử dụng hành động Index của bộ điều khiển Cat.

+0

tôi thực sự đã tạo một RouteHandler, nhưng "rd.Values ​​[" controller "]", rd.GetRequiredString -> cách sửa đổi và nhận dữ liệu tuyến đường đã giúp rất nhiều, và tôi chỉ tìm kiếm điều này. CẢM ƠN. –

Các vấn đề liên quan