Attempting to use F # controllers with the web API

I am getting the error "Error loading MyApp.Api.App from assembly MyApp.Api" in my C # mvc project that is referencing the f # web api library. The C # MyApp.Web project has a reference to the F # MyApp.Api project and has no compilation errors. What could be the problem?

App.fs in the MyApp.Api project

namespace MyApp.Api

open System
open System.Web
open System.Web.Mvc
open System.Web.Routing
open System.Web.Http
open System.Data.Entity
open System.Net.Http.Headers

open System.Net.Http.Headers

type Route = { controller : string; action : string; id : UrlParameter }
type ApiRoute = { id : RouteParameter }

type App() =
    inherit System.Web.HttpApplication() 

    static member RegisterGlobalFilters (filters:GlobalFilterCollection) =
        filters.Add(new HandleErrorAttribute())

    static member RegisterRoutes(routes:RouteCollection) =
        routes.IgnoreRoute( "{resource}.axd/{*pathInfo}" )
        routes.MapHttpRoute( "DefaultApi", "api/{controller}/{id}", 
            { id = RouteParameter.Optional } ) |> ignore
        routes.MapRoute("Default", "{controller}/{action}/{id}", 
            { controller = "Home"; action = "Index"; id = UrlParameter.Optional } ) |> ignore

    member this.Start() =
        AreaRegistration.RegisterAllAreas()
        App.RegisterRoutes RouteTable.Routes
        App.RegisterGlobalFilters GlobalFilters.Filters

      

And my global.asax.cs in MyApp.Web

using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using MyApp.Api;

namespace MyApp.Web
{
    public class WebApiApplication :  MyApp.Api.App// System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            base.Start();
        }

    }
}

      

+3


source to share


1 answer


You are registering your api route incorrectly. While the APIs look the same, they are not. You need to register a web API route with an instance HttpConfiguration

:

GlobalConfiguration.Configuration.Routes.MapHttpRoute("", "", ...)

      

You are trying to map a web API route in MVC RouteTable

. I'm really surprised you don't get a compilation error.


So the above is not the case. I shouldn't have included the appropriate namespace when I tried before without pulling the Dan Mohl project template.



You have subclassed your type MyApp.Api.App

in Global.asax.cs

. The given template does not include this. Instead, its template changes the markup Global.asax

as follows:

<%@ Application Inherits="MyApp.Api.App" Language="C#" %>
<script Language="C#" RunAt="server">

  protected void Application_Start(Object sender, EventArgs e) {
      base.Start();
  }

</script>

      

Everything seems to work fine. I also got the following:

<%@ Application Inherits="MyApp.Web.WebApiApplication" Language="C#" %>
<!-- The following seems to be optional; just an extra, duplicate event handler.
     I was able to run the app with this script and without. -->
<script Language="C#" RunAt="server">

  protected void Application_Start(Object sender, EventArgs e) {
      base.Start();
  }

</script>

      

Note that you need the complete namespace, not just the type name. If this works correctly, then I think more code is needed, since I can't find anything else that is wrong.

+2


source







All Articles