2010-09-04 39 views
0

請在下面找到我的ActionResult指數:指數的ActionResult返回404

public ActionResult Index(string SectionText) 
{ 

    var products = from p in db.Products 
        where p.CategoryID == SectionText 
        //orderby gs.SortBy 
        select p; 

    return View(products.ToList()); 
} 

這是拋出的錯誤如下: - '/' 中應用

服務器錯誤。

無法找到該資源。

說明:HTTP 404資源 你正在尋找(或它的 一個依賴項)可能已被移除, 更名,或者是 暫時不可用。請 查看以下URL並確保 拼寫正確。

請求的URL: /節/子板-894/

任何想法,將不勝感激。這是使用內置和Web服務器。

感謝

回答

0

那麼您請求/Sections/daughterboards-894並沒有任何東西會涉及此網址到Index行動。如果您使用的是默認路由你的要求應該是這樣的:/sections/index/daughterboards-894

當然,這是假設你有能力處理這個URL路徑:

routes.MapRoute(
    "Default", 
    "{controller}/{action}/{SectionText}", 
    new { 
     controller = "Home", 
     action = "Index", 
     SectionText = UrlParameter.Optional 
    } 
); 

如果你想有一個像/Sections/daughterboards-894/的網址您將需要設置一個特殊的路線吧:

routes.MapRoute(
    "MySpecialUrl", 
    "Sections/{SectionText}", 
    new { 
     controller = "Sections", 
     action = "Index", 
     SectionText = UrlParameter.Optional 
    } 
); 

備註:作爲一個方面說明我會建議你抽象的數據訪問到存儲庫,而不是直接在控制器中訪問它。這將使您的代碼更容易進行單元測試。

+0

謝謝我添加了路線,但這並沒有達到預期的效果,我仍然得到相同的錯誤 – ashga 2010-09-04 10:13:31

+0

只是更新此鏈接在這裏工作,但如果我刪除索引它沒有。但索引作爲默認選項指定爲 http:// localhost:21063/Sections/Index/firewalls-hardware-832 - works http:// localhost:21063/Sections/firewalls-hardware-832 - does not work .. – ashga 2010-09-04 10:38:25

+0

謝謝我想出了新的路線工程,但我需要把它放在默認路線學校男孩錯誤! – ashga 2010-09-04 10:45:05