【问题标题】:Index ActionResult returns 404索引 ActionResult 返回 404
【发布时间】:2023-03-20 22:56:01
【问题描述】:

请在下面找到我的 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());
}

这是抛出以下错误:-

“/”应用程序中的服务器错误。

找不到资源。

描述:HTTP 404。资源 您正在寻找(或其中之一 依赖项)可能已被删除, 它的名字被改变了,或者是 暂时不可用。请 查看以下 URL 并确保 它拼写正确。

请求的网址: /Sections/daughterboards-894/

任何想法将不胜感激。这是使用内置的 vs web 服务器。

谢谢

【问题讨论】:

    标签: c# asp.net-mvc-2


    【解决方案1】:

    好吧,您正在请求/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/ 这样的url,你需要为它设置一个特殊的路由:

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

    备注:作为旁注,我建议您将数据访问抽象到存储库中,而不是直接在控制器中访问它。这将使您的代码更容易进行单元测试。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    • 2012-12-08
    • 1970-01-01
    相关资源
    最近更新 更多