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。但我不想改變我的網址模式。
請建議我如何做到這一點,你可以建議一些完全不同的方式,來實現這一
我實際上創建了一個RouteHandler,但是「rd.Values [」controller「]」,rd.GetRequiredString - >修改和獲取路由數據的方法有很多幫助,我只是在尋找這個。謝謝。 –