You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

安装EPiServer ServiceApi后内部API失效问题求助

EPiServer ServiceApi安装后内部Web API失效问题

环境与前置操作

  • 现有系统:EPiServer CMS 11 + Commerce 13
  • 新增组件:ServiceApi(5.6.1)、ServiceApi.Commerce(5.6.1)
  • 启动要求:必须在web.config中添加以下配置才能正常启动站点:
<add key="episerver:serviceapi:maphttpattributeroutes" value="false" />

问题现象

站点启动后,原有内部系统API全部失效,出现以下报错:

  1. DataTable填充API报错
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. 
    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)
  1. QuickOrder API报错
quickorder.ts:52 GET http://localhost:65238/api/quickorder/getProductNameAutocomplete?term=d&size=5 500 (Internal Server Error)

相关配置信息

内部API由WebApiConfig.cs配置,Opti技术支持指出问题可能与缺少XMLMediaTypeFormatter有关——但此前合作方注释了移除XML格式化器的代码,取消注释会导致站点启动时抛出空引用异常。代码如下:

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");
        }
    }
}

提问

请问是否有人遇到过类似问题,或知晓如何恢复内部API的正常运行?


内容的提问来源于stack exchange,提问作者Andrew

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 18:05:05