MVC設計模式不適用於重寫URL以指向文件夾結構。它可以做到這一點,但它當然不是它的主要目的。如果您嘗試使用靜態內容創建URL結構,則使用內置於IIS中的URL rewriting功能可能更容易。
如果您要創建一個完整的MVC應用程序,設置和FoodController
到DrinkController
服務你的意見,例如:
public class FoodController : Controller
{
public ActionResult ViewDishByTag(string itemType, string itemTag)
{
// If an itemType is displayed without itemTag, return an 'index' list of possible dishes...
// Alternatively, either return a "static" view of your page, e.g.
if (itemTag== "pad-thai")
return View("PadThai"); // where PadThai is a view in your shared views folder
// Alternatively, look up the dish information in a database and bind return it to the view
return ("RecipeView", myRepo.GetDishByTag(itemTag));
}
}
使用上面的例子,你的路線可能看起來有點像這樣:
routes.MapRoute(
"myRoute",
"{controller}/{itemType}/{itemTag}",
new
{
controller = UrlParameter.Required,
action = "ViewDishByTag",
itemtype = UrlParameter.Optional,
itemTag = UrlParameter.Optional
}
);
您的問題沒有包含有關您的實施的詳細信息,因此如果您希望我擴展任何內容,請更新您的問題。
感謝您的解釋。 – Dkong 2012-01-16 22:59:10