我是ASP.NET MVC的新手,直到現在,我已經能夠將幾乎所有需要的東西都映射出來。我的URL變得越來越複雜,我可以讓他們路由並做我想做的事情,但我確信我做這件事的方式是錯誤的,我必須這樣做的代碼是非常草率的。幾個是我的網址是:複雜的URL路由C#MVC 2.0
mysite.com/Setup/FieldManager/Client/List
mysite.com/Setup/FieldManager/Client/Add
基本上我想要的網址佈局是Module/SubModule/Type/Event。我的問題是我不得不傳遞給調用Field Manager Action的安裝控制器。從這裏開始,我不得不做一個荒謬的轉換語句,然後從這裏「分管理」路由到其他正確的操作。我場管理器操作是這樣的:
public virtual ActionResult FieldManager(string type, string option, string inputType, string values, string label, string varname, string id)
{
CheckAuth(false);
ActionResult theView = null;
switch (type)
{
case "Client":
switch (option)
{
case "Add":
theView = AddClientField();
break;
case "List":
theView = ClientFieldManager();
break;
case "Save":
theView = SaveClientField(inputType, values, label, varname, id);
break;
default:
theView = EditClientField(option);
break;
}
break;
case "Case":
theView = View("CaseFieldManager");
break;
default:
theView = View("Error");
break;
}
return theView;
}
我想保持我使用相同的URL並直接調用正確的控制器/動作,而無需從一個動作到另一個重新路由呼叫。我設置的當前路線也在下面。
routes.MapRoute(
"Default",
"{controller}/{action}/{type}/{option}",
new { action = "Index", type = UrlParameter.Optional, option = UrlParameter.Optional }
);
routes.MapRoute(
"Root",
"",
new { controller = "Account", action = "Index", id = UrlParameter.Optional }
);
我的工作原理太簡單了,我不得不在通話之間轉換類型。我正在尋找最好的方法來完成我爲什麼要做的事。要做到這一點的最佳方式是爲我的所有電話製作數百條特定路線,還是錯過了某些內容?
這正是我所需要的。我搜索了一下,發現了這兩個鏈接:http://www.asp.net/mvc/videos/aspnet-mvc-2-areas和http://www.asp.net/learn/whitepapers/what-is-new -in-aspnet-mvc,並能夠做我想做的事情。謝謝您的幫助。 – DSlagle 2010-11-08 04:36:00