2015-04-16 18 views
0

我很確定我只是錯過了一些明顯的東西,但是這裏發生了什麼。ASP.NET MVC路由問題 - 視圖'營銷'或其主人沒有被發現或沒有視圖引擎支持搜索的位置

我加入到我的RouteConfig.cs文件的自定義路徑,如下所示:

routes.MapRoute(
    name: "LibraryCategoryList", 
    url: "Library/List/{id}", 
    defaults: new { controller = "Library", action = "List", id = "Marketing" } 
); 
routes.MapRoute(
    name: "Default", 
    url: "{controller}/{action}/{id}", 
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, 
    namespaces: new[] { "BFRDP.Controllers" } 
); 

我有一個名爲圖書館與動作列表控制,具體如下:

public ActionResult List(string id) 
{ 
    return View(id); 
} 

我的列表視圖在〜/ Views/Library/List.cshtml。

當我嘗試去http://localhost:49591/Library/List/Marketing,我得到的錯誤:

The view 'Marketing' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/Views/Library/Marketing.aspx ~/Views/Library/Marketing.ascx ~/Views/Shared/Marketing.aspx ~/Views/Shared/Marketing.ascx ~/Views/Library/Marketing.cshtml ~/Views/Library/Marketing.vbhtml ~/Views/Shared/Marketing.cshtml ~/Views/Shared/Marketing.vbhtml

(是的,我確實有RouteConfig.RegisterRoutes(RouteTable.Routes);在我的Global.asax Application_Start方法中。 cs文件)。

我在做什麼錯了?

謝謝!

Laurie

+2

沒有必要爲LibraryCategoryList路線。第二條路線對於「圖書館/列表/市場營銷」的請求也是一樣的。 –

+1

您使用[View]方法的[this overload](https://msdn.microsoft.com/en-us/library/dd460352(v = vs.118).aspx) - 即指定視圖名稱。如果您想將值「Marketing」作爲模型傳遞給'List.cshtml'視圖,則使用'return View((object)id);' - 即[this overload](https://msdn.microsoft。 com/en-us/library/dd492930(v = vs.118).aspx) –

+0

Stephen - 謝謝,那修好了! –

回答

2

該框架將url與第一個路由相匹配,即LibraryCategoryList。根據這條路線,id參數等於「Marketing」。因此,在您操作方法return語句變成等價的:

return ("Marketing"); 
這種格式的「營銷」

成爲視圖返回的名稱。該框架將在Library文件夾中查找具有此名稱(Marketing.cshtml)的視圖,而不是視圖下的共享文件夾。如果它找不到,它會給你那個錯誤。所以無論是定義一個營銷視圖庫文件夾下,或者返回另一個視圖或代碼更改爲:

return ("List"); 
相關問題