ASP.NET MVC2站点是否可以使用可选的枚举路由参数?如果是这样,如果没有提供,我们可以默认该值吗?

Pur*_*ome 7 .net asp.net-mvc enums routes asp.net-mvc-2

我可以有像...这样的路线吗?

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

行动方法是......

[HttpGet]
public ActionResult Show(int? aaa, int? bbb, LocationType locationType) { ... }
Run Code Online (Sandbox Code Playgroud)

如果此人没有为locationType... 提供值,则默认为LocationType.Unknown.

这可能吗?

更新#1

我已经剥离了动作方法以包含一个方法(直到我得到这个工作).它现在看起来像..

[HttpGet]
public ActionResult Show(LocationType locationType = LocationType.Unknown) { .. }
Run Code Online (Sandbox Code Playgroud)

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

参数字典包含参数'locationType'的无效条目,用于'MyProject.Controllers.GeoSpatialController'中方法'System.Web.Mvc.ActionResult Show(MyProject.Core.LocationType)'.字典包含"System.Int32"类型的值,但该参数需要"MyProject.Core.LocationType"类型的值.参数名称:参数

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

Fab*_*ian 6

您可以提供如下默认值:

public ActionResult Show(int? aaa, int? bbb, LocationType locationType = LocationType.Unknown) { ... }
Run Code Online (Sandbox Code Playgroud)


更新:

或者如果您使用的是.NET 3.5:

public ActionResult Show(int? aaa, int? bbb, [DefaultValue(LocationType.Unknown)] LocationType locationType) { ... }
Run Code Online (Sandbox Code Playgroud)


更新2:

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

public enum LocationType {
    Unknown = 0,
    Something = 1
}
Run Code Online (Sandbox Code Playgroud)