Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 索引ActionResult返回404_C#_Asp.net Mvc 2 - Fatal编程技术网

C# 索引ActionResult返回404

C# 索引ActionResult返回404,c#,asp.net-mvc-2,C#,Asp.net Mvc 2,请在下面找到我的ActionResult索引: public ActionResult Index(string SectionText) { var products = from p in db.Products where p.CategoryID == SectionText //orderby gs.SortBy select p; return Vie

请在下面找到我的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。资源 您正在寻找(或其中一个) 依赖项)可以被删除, 它的名字改变了,或者 暂时不可用。请 查看以下URL并确保 它拼写正确

请求的URL: /截面图/子板-894/

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


谢谢

您正在请求
/Sections/daughterboards-894
,并且没有任何内容会将此url与
索引
操作关联。如果您使用的是默认路由,您的请求应该如下所示:
/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 
    }
);

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

谢谢,我添加了路由,但没有达到预期效果,我仍然会收到相同的错误。只是更新此链接可以工作,但如果我删除索引,它就不会工作。但是,索引被指定为默认选项-工作-不工作…谢谢,我发现新路线可行,但我需要将其放在默认路线校童错误之前!