Installing ServiceApi breaks all our internal API's

Vote:
 

Hi,

We are on CMS 11 and Commerce 13. I just got the ServiceApi (5.6.1) and ServiceApi.Commerce (5.6.1) installed on our site and with some configuration enabled the site to start without errors. The site wouldn't start until I added the following line to my web.config.

<add key="episerver:serviceapi:maphttpattributeroutes" value="false" />


However, after these steps, our internal system APIs are broken when they worked before. There is a WebApiConfig.cs file that is what sets up our internal APIs but I'm not sure what would fix this. When sending a query off that connects to our API and populates a datatable, these are the errors we get.

GET http://localhost:65238/api/library/list?sEcho=2&iColumns=4&sColumn…ow&bRegex=false&iSortCol_0=0&sSortDir_0=asc&iSortingCols=1&_=1719429175236 500 (Internal Server Error)
Uncaught Error: DataTables warning: table id=DT_MSDSList - Ajax error. For more information about this error, please see http://datatables.net/tn/7
    at Ot (master.min.js:27:2081)
    at Object.error (master.min.js:26:14625)
    at u (master.min.js:3:7199)
    at Object.fireWith [as rejectWith] (master.min.js:3:7948)
    at n (master.min.js:4:11376)
    at XMLHttpRequest.<anonymous> (master.min.js:4:16221)


Another error thrown during trying the QuickOrder, which is API on the site

quickorder.ts:52 

GET http://localhost:65238/api/quickorder/getProductNameAutocomplete?term=d&size=5 500 (Internal Server Error)



WebApiConfig.cs file that is the only place I see anything API related in the entire solution. Opti support said something about there being no XMLMediaTypeFormatter, which was being removed in the WebApiConfig.cs for reasons I'm unaware of by a past partner. Commenting out that line was the last step to get the site to start without an exception.

using System;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Web.Http;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using Newtonsoft.Json;

namespace CMS.Ellsworth.Business.Initialization
{
    [InitializableModule]
    [ModuleDependency(typeof(FrameworkInitialization))]
    public class WebApiConfig : IInitializableModule
    {
        public static string ApiRoute = "api";

        public void Initialize(InitializationEngine context)
        {
            // Enable Web API routing
            GlobalConfiguration.Configure(config =>
            {
                config.MapHttpAttributeRoutes();

                var formatters = GlobalConfiguration.Configuration.Formatters;
                var jsonFormatter = formatters.JsonFormatter;
                var settings = jsonFormatter.SerializerSettings;

                var enumConverter = new Newtonsoft.Json.Converters.StringEnumConverter();
                jsonFormatter.SerializerSettings.Converters.Add(enumConverter);
                config.Formatters.Add(new BrowserJsonFormatter());

                settings.Formatting = Formatting.Indented;

                // Commented out due to it throws System.NullReferenceException: Object reference not set to an instance of an object.
                // If its uncommented, it will throw an exception when the site is started.
                // config.Formatters.Remove(config.Formatters.XmlFormatter);

                config.Routes.MapHttpRoute(
                    name: "DefaultEpiApi",
                    routeTemplate: ApiRoute + "/{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional });
            });
        }

        public void Uninitialize(InitializationEngine context)
        {
        }
    }

    public class BrowserJsonFormatter : JsonMediaTypeFormatter
    {
        public BrowserJsonFormatter()
        {
            SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
            SerializerSettings.Formatting = Formatting.Indented;
        }

        public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType)
        {
            base.SetDefaultContentHeaders(type, headers, mediaType);
            headers.ContentType = new MediaTypeHeaderValue("application/json");
        }
    }
}


Has anyone encountered this or know what to do to get my API's to work again?

#324365
Edited, Jun 26, 2024 19:30
Vote:
 

Do your own API's rely on HTTP attribute routing? Is it enabled already in your code?

Could it be related to that custom BrowserJsonFormatter? Looks like you add another JSON formatter, besides the built-in one.

#325065
Jul 08, 2024 8:45
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.