can I have a route like...
我可以有像...这样的路线吗?
routes.MapRoute(
"Boundaries-Show",
"Boundaries",
new
{
controller = "Boundaries",
action = "Show",
locationType = UrlParameter.Optional
});
Where the action method is...
行动方法是......
[HttpGet]
public ActionResult Show(int? aaa, int? bbb, LocationType locationType) { ... }
and if the person doesn't provide a value for locationType
.. then it defaults to LocationType.Unknown
.
如果此人没有为locationType提供值,则默认为LocationType.Unknown。
Is this possible?
这可能吗?
Update #1
I've stripped back the action method to contain ONE method (just until I get this working). It now looks like this ..
我已经剥离了动作方法以包含一个方法(直到我得到这个工作)。它现在看起来像..
[HttpGet]
public ActionResult Show(LocationType locationType = LocationType.Unknown) { .. }
.. and I get this error message...
..我收到此错误消息...
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'的无效条目,用于'MyProject.Controllers.GeoSpatialController'中方法'System.Web.Mvc.ActionResult Show(MyProject.Core.LocationType)'。字典包含“System.Int32”类型的值,但该参数需要“MyProject.Core.LocationType”类型的值。参数名称:参数
Is it thinking that the optional route parameter LocationType
is an int32 and not a custom Enum
?
是否认为可选路由参数LocationType是int32而不是自定义Enum?
2 个解决方案
#1
6
You can supply a default value like so:
您可以提供如下默认值:
public ActionResult Show(int? aaa, int? bbb, LocationType locationType = LocationType.Unknown) { ... }
UPDATE:
更新:
Or if you are using .NET 3.5:
或者如果您使用的是.NET 3.5:
public ActionResult Show(int? aaa, int? bbb, [DefaultValue(LocationType.Unknown)] LocationType locationType) { ... }
UPDATE 2:
更新2:
public ActionResult Show(int? aaa, int? bbb, int locationType = 0) {
var _locationType = (LocationType)locationType;
}
public enum LocationType {
Unknown = 0,
Something = 1
}
#2
0
You can add the DefaultValue attribute to your action method:
您可以将DefaultValue属性添加到您的操作方法:
[HttpGet]
public ActionResult Show(int? aaa, int? bbb, [DefaultValue(LocationType.Unknown)]LocationType locationType) { ... }
or use optional parameters depending on what language version your using:
或使用可选参数,具体取决于您使用的语言版本:
[HttpGet]
public ActionResult Show(int? aaa, int? bbb, LocationType locationType = LocationType.Default) { ... }
#1
6
You can supply a default value like so:
您可以提供如下默认值:
public ActionResult Show(int? aaa, int? bbb, LocationType locationType = LocationType.Unknown) { ... }
UPDATE:
更新:
Or if you are using .NET 3.5:
或者如果您使用的是.NET 3.5:
public ActionResult Show(int? aaa, int? bbb, [DefaultValue(LocationType.Unknown)] LocationType locationType) { ... }
UPDATE 2:
更新2:
public ActionResult Show(int? aaa, int? bbb, int locationType = 0) {
var _locationType = (LocationType)locationType;
}
public enum LocationType {
Unknown = 0,
Something = 1
}
#2
0
You can add the DefaultValue attribute to your action method:
您可以将DefaultValue属性添加到您的操作方法:
[HttpGet]
public ActionResult Show(int? aaa, int? bbb, [DefaultValue(LocationType.Unknown)]LocationType locationType) { ... }
or use optional parameters depending on what language version your using:
或使用可选参数,具体取决于您使用的语言版本:
[HttpGet]
public ActionResult Show(int? aaa, int? bbb, LocationType locationType = LocationType.Default) { ... }