2011-07-10 186 views
6

注意改變URL:下面僅僅是一個小的演示排序模擬什麼,我在尋找:重定向到另一個控制器+動作,而在ASP.Net MVC3

下面是我的應用程序,用戶可以將網址格式見

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模式類型,我打算創建格式短網址如下

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。但我不想改變我的網址模式。

請建議我如何做到這一點,你可以建議一些完全不同的方式,來實現這一

回答

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方法將EXEC會獲取id = 1,然後它將使用Cat控制器的Index操作。

+0

我實際上創建了一個RouteHandler,但是「rd.Values [」controller「]」,rd.GetRequiredString - >修改和獲取路由數據的方法有很多幫助,我只是在尋找這個。謝謝。 –

相關問題