gpt4 book ai didi

c# - GETing整数参数时MVC路由问题

转载 作者:行者123 更新时间:2023-11-30 15:30:52 27 4
gpt4 key购买 nike

我不知道为什么会出现此路由问题。

Global.asax.cs 文件中的路由:

routes.MapRoute(
"Archives", //Route name
"{controller}/{action}/{month}", // URL with parameters
new { controller = "Articles", action = "Archives" },
new { month = @"^\d+" } // Parameter.defaults
);

Controller :

public ActionResult Archives(int month)
{
ViewData["month"] = month;
return View(article);
}

不断抛出错误:

The parameters dictionary contains a null entry for parameter 'month' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Archives(Int32)' in 'AppleWeb.Controllers.ArticlesController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

这是假的,因为 URL 是:http://localhost:64529/Articles/Archives/12

编辑 - 所有人都能看到的完整路由:

public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//routes.IgnoreRoute("tellerSurvey.htm/{*pathInfo}");routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Appleweb", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

routes.MapRoute(
"Contact", //Route name
"{controller}/{action}/{page}", //URL with parameters
new { controller = "Appleweb", action = "Contact", page = UrlParameter.Optional } // Paramter defaults
);

routes.MapRoute(
"FormDetails", //Route name
"{controller}/{action}/{formid}", // URL with parameters
new { controller = "Resources", action = "FormDetails", formid = 0}
);

routes.MapRoute(
"_calc",
"{controller}/{action}/{calcid}", // URL with parameters
new { controller = "Resources", action = "Calc", calcid = UrlParameter.Optional } // Parameter defaults
);

routes.MapRoute(
"Article", //Route name
"{controller}/{action}/{articleid}", // URL with parameters
new { controller = "Articles", action = "Article", id = 0 } // Parameter.defaults
);}

这是一个 MVC 3 项目,所以没有 routingconfig.cs。

最佳答案

问题来了:URL http://localhost:64529/Articles/Archives/12 匹配其他路由。它将匹配 Default、Contact 等路由。

编辑最简单的解决方案

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

routes.MapRoute(
"Archives",
"{controller}/{action}/{month}",
new { controller = "Articles", action = "Archives" },
new { month = @"\d+"}
);

//short-circuit for all URLs where the month is not numeric
//Warning: the 404 will not be handled in <customErrors>
//Its handled in <httpErrors>
routes.IgnoreRoute("Articles/Archives/{month}");

//place all other routes here

其他可能性

1) 将 {controller} 替换为硬编码的 Controller 名称,这样 /Articles 就不会匹配路由。示例:

    routes.MapRoute(
"Contact",
"Appleweb/{action}/{page}",
new { controller = "Appleweb", action = "Contact", page = UrlParameter.Optional }
);

只匹配以/Appleweb开头的网址

2)使用约束

    routes.MapRoute(
"Archives", //Route name
"Appleweb/{action}/{page}",
new { controller = "Appleweb", action = "Contact", page = UrlParameter.Optional },
new { controller = "^(?!articles$).*$" } //don't match articles
);

    routes.MapRoute(
"Archives", //Route name
"Appleweb/{action}/{page}",
new { controller = "Appleweb", action = "Contact", page = UrlParameter.Optional },
new { controller = "appleweb|resources" } //only allow appleweb and resources
);

3) 使存档的 URL 唯一,如 http://XXXX/Archives/12

   routes.MapRoute(
"Archives",
"Archives/{month}",
new { controller = "Articles", action = "Archives" },
new { month = @"\d+" }
);

关于c# - GETing整数参数时MVC路由问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21532200/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com