Mar*_*cky 3 asp.net-mvc f# ninject
我相应地创建带有可选参数的控制器:
type ProductController(repository : IProductRepository) =
inherit Controller()
member this.List (?page1 : int) =
let page = defaultArg page1 1
Run Code Online (Sandbox Code Playgroud)
当我启动应用程序时,它给了我错误:“ System.MissingMethodException:没有为此对象定义无参数构造函数。 ”
我知道依赖注入的这个错误,这是我的 Ninject 设置:
static let RegisterServices(kernel: IKernel) =
System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver <- new NinjectResolver(kernel)
let instance = Mock<IProductRepository>()
.Setup(fun m -> <@ m.Products @>)
.Returns([
new Product(1, "Football", "", 25M, "");
new Product(2, "Surf board", "", 179M, "");
new Product(3, "Running shoes", "", 95M, "")
]).Create()
kernel.Bind<IProductRepository>().ToConstant(instance) |> ignore
do()
Run Code Online (Sandbox Code Playgroud)
问题是当我从控制器中删除我的可选参数时一切正常。当更改常规参数时,它给我以下错误: 参数字典包含一个空条目,用于方法 'System.Web.Mvc.ViewResult List(Int32)' 的不可为空类型 'System.Int32' 的参数 'page' 'FSharpStore.WebUI.ProductController'。可选参数必须是引用类型、可为空类型或声明为可选参数。参数名称:参数
这是我的路线设置:
static member RegisterRoutes(routes:RouteCollection) =
routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
{ controller = "Product"; action = "List"; id = UrlParameter.Optional } // Parameter defaults
) |> ignore
Run Code Online (Sandbox Code Playgroud)
有没有人为控制器做过可选参数?我正在为我的同事开展试点项目,以将 F# 推广到我们的堆栈。谢谢
另一个 F# - C# 互操作痛苦:P
F# 可选参数是 F# 调用者的有效可选参数,但在 C# 中,此参数将是FsharpOption<T>对象。
在您的情况下,您必须使用Nullable<T>. 所以,代码看起来像:
open System.Web.Mvc
open System
[<HandleError>]
type HomeController() =
inherit Controller()
member this.Hello(code:Nullable<int>) =
this.View() :> ActionResult
Run Code Online (Sandbox Code Playgroud)