Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9234127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:43:00+00:00 2026-06-18T06:43:00+00:00

I have a ASP.NET MVC Web API controller that returns public IEnumerable<IMessage> Get() It

  • 0

I have a ASP.NET MVC Web API controller that returns public IEnumerable<IMessage> Get()

It throws exception that I need to register types that derive from IMessage in known types collection passed to DataContractSerializer.

How to register “Known Types” for using with DataContractSerializer and DataContractJSONSerializer in MVC Web API project?

KnownType attribute cannot be placed on interface.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T06:43:02+00:00Added an answer on June 18, 2026 at 6:43 am

    You need to put the KnownTypeAttribute on your IMessage implementations:

    public interface  IMessage
    {
        string Content { get; }
    }
    
    [KnownType(typeof(Message))]
    public class Message : IMessage {
        public string Content{ get; set; }
    }
    
    [KnownType(typeof(Message2))]
    public class Message2 : IMessage
    {
        public string Content { get; set; }
    }
    

    So when calling the following action:

     public IEnumerable<IMessage> Get()
     {
         return new IMessage[] { new Message { Content = "value1" }, 
                                 new Message2 { Content = "value2" } };
     }
    

    The result will be this:

    <ArrayOfanyType xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
        <anyType xmlns:d2p1="http://schemas.datacontract.org/2004/07/MvcApplication3.Controllers" i:type="d2p1:Message">
            <d2p1:Content>value1</d2p1:Content>
        </anyType>
        <anyType xmlns:d2p1="http://schemas.datacontract.org/2004/07/MvcApplication3.Controllers" i:type="d2p1:Message2">
           <d2p1:Content>value2</d2p1:Content>
        </anyType>
    </ArrayOfanyType>
    

    But this will only work one “way”. So you cannot post back the same XML.

    In order to the following action should work:

    public string Post(IEnumerable<IMessage> messages)
    

    You need to register the known types globally, with configuring a DataContractSerializer and setting up in the GlobalConfiguration.Configuration.Formatters

    GlobalConfiguration.Configuration
                       .Formatters
                       .XmlFormatter.SetSerializer<IEnumerable<IMessage>>(
                           new DataContractSerializer(typeof(IEnumerable<IMessage>), 
                               new[] { typeof(Message), typeof(Message2) }));
    

    With using the configuration you don’t need the KnownTypeAttribute on your implementation types.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created an ASP.NET web API controller that is returning a strongly typed
I have a layered web application that I built with ASP.NET MVC 4, WebAPI
I have created the hello world of ASP.NET MVC web API projects using VS2012:
I am using web api with ASP.NET MVC 4. I have the following named
I am using ASP.NET MVC 4 with Web Api I have the following ApiController.
I have an ASP.NET MVC 4 application which uses the new ASP.NET Web API.
In an ASP.NET Web API I have modified the default GET action to accept
I have an ASP.NET MVC web app and have a function that I need
So I have an ASP.NET MVC web application that has a silverlight application embedded
I have an ASP.NET MVC 4 Web API app using EntityFramework for ORM. In

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.