簡體   English   中英

索引ActionResult返回404

[英]Index ActionResult returns 404

請在下面找到我的ActionResult索引:

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());
}

這將引發以下錯誤:-

“ /”應用程序中的服務器錯誤。

無法找到該資源。

說明:HTTP404。您正在尋找的資源(或其依賴項之一)可能已被刪除,名稱更改或暫時不可用。 請查看以下網址,並確保其拼寫正確。

要求的網址:/ Sections / daughterboards-894 /

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

謝謝

好吧,您正在請求/Sections/daughterboards-894 ,沒有任何將該URL與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 
    }
);

備注:作為旁注,我建議您將數據訪問抽象到存儲庫中,而不是直接在控制器中訪問它。 這將使您的代碼更容易進行單元測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM