Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.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# Action()包含不需要的参数,试图获得更多控制权_C#_.net_Asp.net Mvc 3_Routing_Url.action - Fatal编程技术网

C# Action()包含不需要的参数,试图获得更多控制权

C# Action()包含不需要的参数,试图获得更多控制权,c#,.net,asp.net-mvc-3,routing,url.action,C#,.net,Asp.net Mvc 3,Routing,Url.action,我在页面中有一个链接,如下所示: Url.Action("List", "Item", new { category = "seasons" }) Url.Action("List", "Item", new { category = "seasons", group = "", parentGroup = "" }) 与该页匹配的路由还具有parentGroup和group参数 例如:/seasures/Moober/Blue/1->/{category}/{parentGroup}/{g

我在页面中有一个链接,如下所示:

Url.Action("List", "Item", new { category = "seasons" })
Url.Action("List", "Item", new { category = "seasons", group = "", parentGroup = "" })
与该页匹配的路由还具有parentGroup和group参数

例如:/seasures/Moober/Blue/1->/{category}/{parentGroup}/{group}/{id}

问题是,当我在该页面上并使用Url.Action时,它会添加所有缺少的路由值,即使我尝试生成指向categories only/{category}的链接,它仍会添加parentGroup和group

我发现,这样做意味着:

Url.Action("List", "Item", new { category = "seasons" })
Url.Action("List", "Item", new { category = "seasons", group = "", parentGroup = "" })
但它不适用于我,因为它将它们从我的url中删除,但将它们作为参数添加: /季节?父组=穆伯&组=蓝色

我正在使用MVC3。 有没有办法强制Url.Action()只使用提供的参数或取消我不想要的参数

这是路线

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
    "ItemFromCategoryParentGroupSubGroup", // Route name
    "{category}/{parentGroup}/{group}/{id}/{language}", // URL with parameters
    new { controller = "Item", action = "Show", page = 1, language = ConfigurationManager.AppSettings["DefaultLanguage"] }, // Parameter defaults
    new
    {
        category = _validCategory,
        parentGroup = _validGroup,
        group = _validChildGroup,
        id = _validItemInChildGroup
    }
);

routes.MapRoute(
    "ItemListFromParentGroup", // Route name
    "{category}/{parentGroup}/{group}/{language}", // URL with parameters
    new { controller = "Item", action = "List", page = 1, language = ConfigurationManager.AppSettings["DefaultLanguage"] }, // Parameter defaults
    new
    {
        category = _validCategory,
        parentGroup = _validGroup,
        group = _validChildGroup
    }
);

routes.MapRoute(
    "ItemWithGroup", // Route name
    "{category}/{group}/{id}/{language}", // URL with parameters
    new { controller = "Item", action = "Show", page = 1, language = ConfigurationManager.AppSettings["DefaultLanguage"] }, // Parameter defaults
    new
    {
        category = _validCategory,
        group = _validGroup,
        id = _validItemInGroup
    }
);

routes.MapRoute(
    "ItemListWithGroup", // Route name
    "{category}/{group}/{language}", // URL with parameters
    new { controller = "Item", action = "List", page = 1, language = ConfigurationManager.AppSettings["DefaultLanguage"] }, // Parameter defaults
    new
    {
        category = _validCategory,
        group = _validGroup
    }
);

routes.MapRoute(
    "ItemListFromCategory", // Route name
    "{category}/{language}", // URL with parameters
    new { controller = "Item", action = "List", page = 1, language = ConfigurationManager.AppSettings["DefaultLanguage"] }, // Parameter defaults
    new
    {
        category = _validCategory
    }
);
编辑:

目前我有一个解决方案,如下所示: Url.RouteUrl(“ItemListFromCategory”) 我基本上是在强迫你走一条应该与之相匹配的路线 动作(“列表”、“项目”,新的{category=“seasures”}) 这次没有自动添加任何参数


问题是,我被迫使用命名路由。

尝试将其他参数声明为urlparmeter。在路由注册期间为可选参数,并在操作中为这些参数设置默认值。例如

MyAction(string category, string group = "", parentGroup = 0)
希望这有帮助。

改变路线

routes.MapRoute(
    "SomeRoute", // Route name
    "/{category}/{parentGroup}/{group}/{id}", 
    new { 
        controller = "DefaultController", 
        action = "DefaultAction", 
        parentGroup = UrlParameter.Optional
        group = UrlParameter.Optional
        id= UrlParameter.Optional
    }
);

使parentGroup、parentGroup、group
成为可选的

最后,我为每个路由构建自定义约束


我发现它几乎可以完全控制我们希望对路由执行的任何操作。

您不能有多个可选参数,除非您执行以下操作:我已将此解决方法用于多个可选和连续参数,但最终生成的url仍然相同,我会试着把它和弗兰基的建议结合起来。我可以,我只是希望它们不会太长。