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

  • SEARCH
  • Home
  • 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 9213265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:42:21+00:00 2026-06-18T01:42:21+00:00

I have a MVC4 Web API service which works fine for concrete data types.

  • 0

I have a MVC4 Web API service which works fine for concrete data types. But when I specify an interface as a contract and try to return the concrete type from the action, it throws an error

public interface IData
{
  string NameText {get;set;}
}

[KnownType(typeof(IData))]
public class Data : IData 
{
  string NameText {get;set;}
}

The ‘ObjectContent`1’ type failed to serialize the response body for
content type ‘application/xml; charset=utf-8’.

Consider using a DataContractResolver or add any types not known
statically to the list of known types – for example, by using the
KnownTypeAttribute attribute or by adding them to the list of known
types passed to DataContractSerializer.

I tried adding a KnownType contract over the concrete type specifying the interface type but it doesnt work.

Is it not possible to specify interfaces as DataContracts for the Service in MVC Web API? This used to work in WCF

  • 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-18T01:42:22+00:00Added an answer on June 18, 2026 at 1:42 am

    [KnownType] is for specifying concrete classes, not interfaces. If you specify which classes implement IData with [KnownType], then at least MVC knows what types it will have to serialize/deserialize:

    [KnownType(typeof(MyDataThingy1))]
    public class MyDataThingy1 : IData
    { ... }
    
    [KnownType(typeof(MyDataThingy2))]
    public class MyDataThingy2 : IData
    { ... }
    

    This should do the trick!

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

Sidebar

Related Questions

I have been catching up with MVC4 Web API and WCF Data Service. From
I have a ASP.NET MVC4 web api that uses fluent NHibernate, but when I
I develop new restful service using MVC4 web api for iphone application but I'm
I have a service implemented in MVC4 / ASP.NET Web Api. I would like
I have trying to learn MVC4 Web API. I am running my project from
I am currently working on MVC4 SinglePage Application. I have a Web Api method
I have planned to go for asp.net MVC4 web API. Any one can compare
I am starting to use MVC4 Web API project, I have controller with multiple
I have a WP7 project, which will invoke a REST web service in Azure
We have a Web API (MVC4) application that returns images from the database. I

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.