MVC with extra parameter in F #

I am creating a controller with an additional parameter accordingly:

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

      

When I ran the application, it gave me an error: " System.MissingMethodException: No parameterless constructor is defined for this object. "

I know this error from dependency injection, here are 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 problem is that I remove my optional parameter from the controller, everything works fine. When change parameter for regular value gives me the following error: The parameter dictionary contains a null entry for a non-nullable page parameter of type "System.Int32" for the "System.Web.Mvc.ViewResult List (Int32)" method in "FSharpStore.WebUI .ProductController ". The optional parameter must be a reference type, type null, or declared as an optional parameter. Parameter name: parameters

These are 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

      

Has anyone made an optional parameter for the controller? I am working on a pilot project for my colleague promoting F # to our stack. Thanks to

+3


source to share


1 answer


Another pain in F # - C #: P

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

objects.



In your case, you should use Nullable<T>

. So the code looks like this:

open System.Web.Mvc
open System
[<HandleError>]
type HomeController() =
    inherit Controller()    
    member this.Hello(code:Nullable<int>) =        
        this.View() :> ActionResult

      

+3


source







All Articles