gpt4 book ai didi

.net - ASP.NET MVC2 站点可以有一个可选的枚举路由参数吗?如果是这样,如果未提供,我们可以默认该值吗?

转载 作者:行者123 更新时间:2023-12-04 12:14:19 26 4
gpt4 key购买 nike

我可以有一条路线吗...

routes.MapRoute(
"Boundaries-Show",
"Boundaries",
new
{
controller = "Boundaries",
action = "Show",
locationType = UrlParameter.Optional
});

Action 方法在哪里...
[HttpGet]
public ActionResult Show(int? aaa, int? bbb, LocationType locationType) { ... }

如果此人没有提供 locationType 的值.. 那么它默认为 LocationType.Unknown .

这可能吗?

更新 #1

我已经剥离了 action 方法以包含一个方法(直到我开始工作)。现在看起来像这样..
[HttpGet]
public ActionResult Show(LocationType locationType = LocationType.Unknown) { .. }

.. 我收到此错误消息...

The parameters dictionary contains an invalid entry for parameter 'locationType' for method 'System.Web.Mvc.ActionResult Show(MyProject.Core.LocationType)' in 'MyProject.Controllers.GeoSpatialController'. The dictionary contains a value of type 'System.Int32', but the parameter requires a value of type 'MyProject.Core.LocationType'. Parameter name: parameters



是否认为可选路由参数 LocationType是 int32 而不是自定义 Enum ?

最佳答案

您可以像这样提供默认值:

public ActionResult Show(int? aaa, int? bbb, LocationType locationType = LocationType.Unknown) { ... }

更新:

或者,如果您使用的是 .NET 3.5:
public ActionResult Show(int? aaa, int? bbb, [DefaultValue(LocationType.Unknown)] LocationType locationType) { ... }

更新 2:
public ActionResult Show(int? aaa, int? bbb, int locationType = 0) {
var _locationType = (LocationType)locationType;
}

public enum LocationType {
Unknown = 0,
Something = 1
}

关于.net - ASP.NET MVC2 站点可以有一个可选的枚举路由参数吗?如果是这样,如果未提供,我们可以默认该值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3415842/

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