Adjust MVC 4 WebApi XmlSerializer to lose the nameSpace

前端 未结 4 1832
温柔的废话
温柔的废话 2020-12-05 15:28

I\'m working on a MVC WebAPI, that uses EF with POCO classes for storage. What I want to do is get rid of the namespace from the XML, so that the endpoints would return and

4条回答
  •  独厮守ぢ
    2020-12-05 16:10

    It's been awhile since I messed with MVC 4, but we ended up replacing the default formatter with the XmlSerializer like so:

    protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
    
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
    
            GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings = GetSerializeSettings();
            GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;
        }
    
        internal JsonSerializerSettings GetSerializeSettings()
        {
            return new JsonSerializerSettings
                               {
                                   Formatting = Formatting.Indented,
                                   ContractResolver = new CamelCasePropertyNamesContractResolver(),
                                   Converters = new List { new IsoDateTimeConverter() }
                               };
        }
    

    This might help... I know we also customized the property names using attributes on the POCOs which you said you don't want to do, but that's because we wanted them to be camel-cased.

提交回复
热议问题