3

I create controller with optional parameter accordingly:

type ProductController(repository : IProductRepository) =
inherit Controller()
member this.List (?page1 : int) = 
    let page = defaultArg page1 1

When I have started application it gives me error: "System.MissingMethodException: No parameterless constructor defined for this object."

I know this error from dependency injection, here is my Ninject settings:

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()

The issue is when I remove my optional parameter from controller all is working fine. When change parameter for regular one it give me following error: The parameters dictionary contains a null entry for parameter 'page' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ViewResult List(Int32)' in 'FSharpStore.WebUI.ProductController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

This is my routes settings:

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

Have anyone done optional parameter for controller? I'm working on pilot project for my colleague to promote F# to our stack. Thanks

2
  • Do you also get the error message if you turn the page1 argument into a regular int, instead of an option? Sep 16, 2014 at 17:54
  • What does your route configuration look like? How do you call your Controller? Sep 16, 2014 at 19:27

1 Answer 1

5

Another F# - C# interop pain :P

The F# optional parameter is a valid optional parameter for F# callers, however in C# this parameters will be FsharpOption<T> objects.

In your case you must use a Nullable<T>. So, the code looks like:

open System.Web.Mvc
open System
[<HandleError>]
type HomeController() =
    inherit Controller()    
    member this.Hello(code:Nullable<int>) =        
        this.View() :> ActionResult
1
  • When specifying this parameter as Nullable<>, I had to explicitly specify the parameter as "null" in the call, or I would get the error "No action was found on the controller '<name>' that matches the request." In order to be able to leave it off, I had to explicitly specify a method with no parameters in the F#. Apr 27, 2015 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.