I have normal WebApi controller that with methods:
public class ModelController : ApiController
{
public IEnumerable<Model> Get()
{
return service.Get();
}
public string Get(int id)
{
return service.Get(id);
}
public void Post([FromBody]MyViewModel value)
{
return service.Save(value);
}
public void Put(int id, [FromBody]MyViewModel value)
{
return service.Save(value);
}
public void Delete(int id)
{
return service.Delete(value);
}
}
I want to wrap return results in to custom object ServiceResult
public class ServiceResult<T>
{
public bool IsError{ get; set; }
public T ObjectModel {get; set}
}
I want to change the return type on all my controller methods to ServiceResult
Here is an example:
public ServiceResult Post([FromBody]MyViewModel value)
{
service.Save(value);
//getting error here:
return new ServiceResult() { IsError= true, ObjectModel = value };
}
But getting the error
The 'ObjectContent`1' type failed to serialize the response body for
content type 'application/xml; charset=utf-8'.
Type 'Models.View.MyViewModel' with
data contract name
'MyViewModel://schemas.datacontract.org/2004/07/MyViewModel'
is not expected. 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.
Do you have any idea how can I fix that?
Your ServiceResult object requires a type argument, Try this
return new ServiceResult<MyViewModel>() { IsError= true, ObjectModel = value };
Related
I have an API with multiple endpoints. I'd like to add a property to all endpoint responses, without adding it to each endpoint response model individually.
Ex:
public class MyClass
{
public string MyProperty { get; set; } = "Hello";
}
public class MyOtherClass
{
public string MyOtherProperty { get; set; } = "World";
}
public class MyController : ControllerBase
{
[HttpPost]
public async Task<ActionResult<MyClass>> EndpointOne(POSTData data)
{
// implementation omitted
}
[HttpPost]
public async Task<ActionResult<MyOtherClass>> EndpointTwo(POSTOtherData otherData)
{
// implementation omitted
}
}
Calling either endpoint returns a JSON representation of MyClass or MyOtherClass as appropriate - i.e.
{ "MyProperty":"Hello" } or { "MyOtherProperty":"World" }
I want to add a property, say a string ApiName, to all endpoints in the API, so that the result of the above code would be either (as appropriate)
{ "MyProperty":"Hello", "ApiName":"My awesome API" }
or
{ "MyOtherProperty":"World", "ApiName":"My awesome API" }
Is there a way to hook into the JSON-stringified result just before returning and add a top-level property like that? If so, I presume I'd have to wire it up in startup.cs, so I've been looking at app.UseEndpoints(...) methods, but haven't found anything that's worked so far. Either it's not added the property, or it's replaced the original result with the new property.
Thanks in advance!
Use Newtonsoft.Json in your net web api
Register a custom contract resolver in Startup.cs:
builder.Services.AddControllers()
.AddNewtonsoftJson(options => options.SerializerSettings.ContractResolver = CustomContractResolver.Instance);
The implementation:
public class CustomContractResolver : DefaultContractResolver {
public static CustomContractResolver Instance { get; } = new CustomContractResolver();
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
var properties = base.CreateProperties(type, memberSerialization);
// add new property
...
properties.Add(newProp);
return properties;
}}
See more Json.net Add property to every class containing of a certain type
You can add a base class with the shared property. Should work for both XML and JSON.
public class MyApiClass
{
public string ApiName => "MyAwesomeApi";
}
public class MyClass : MyApiClass
{
public string MyProperty { get; set; } = "Hello";
}
public class MyOtherClass : MyApiClass
{
public string MyOtherProperty { get; set; } = "World";
}
public class MyController : ControllerBase
{
[HttpPost]
public async Task<ActionResult<MyClass>> EndpointOne(POSTData data)
{
// implementation omitted
}
[HttpPost]
public async Task<ActionResult<MyOtherClass>> EndpointTwo(POSTOtherData otherData)
{
// implementation omitted
}
}
My 0.02 cents says to implement an abstract base class.
Abstract class inheritance look similar to a standard inheritance.
public class MyClass:MyAbstractClass
{
[JsonPropertyName("Class Property")]
public string MyProperty { get; set; } = "Hello";
}
public class MyOtherClass:MyAbstractClass
{
[JsonPropertyName("Class Property")]
public string MyOtherProperty { get; set; } = "World";
}
However the abstract class will allow you to implement additional features in the event you need them in the future.
public abstract class MyAbstractClass{
[JsonPropertyName("API Name")]
public string ApiName{get;set;}="My Aweomse API";
//Just a thought if you want to keep track of the end point names
//while keeping your object names the same
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
public string EndPointName{
get{
return get_endpoint_name();
}}
private string get_endpoint_name(){
return this.GetType().Name;
}
//May as well make it easy to grab the JSON
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
public string As_JSON{
get {
return to_json();
}}
private string to_json(){
object _myObject = this;
string _out;
JsonSerializerOptions options =
new JsonSerializerOptions {
WriteIndented = true };
_out =
JsonSerializer.Serialize(_myObject, options);
return _out;
}
}
Probably should have implemented a generic return object, then you could just loop through the task results. I suppose you still can if you have the task return only the JSON string.
public static void run(){
Task<MyClass> _t0 = task0();
Task<MyOtherClass> _t1 = task1();
Task[] _tasks = new Task[]{_t0,_t1};
Task.WhenAll(_tasks).Wait();
Console.WriteLine(""
+$"{_t1.Result.ApiName}:\n"
+$"End Point: {_t1.Result.EndPointName}:\n"
+$"JSON:\n{_t1.Result.As_JSON}");
Console.WriteLine(""
+$"{_t0.Result.ApiName}:\n"
+$"End Point: {_t0.Result.EndPointName}:\n"
+$"JSON:\n{_t0.Result.As_JSON}");
}
private static Task<MyClass> task0(){
return Task.Run(()=>{
Console.WriteLine("Task 0 Doing Something");
return new MyClass();
});
}
private static Task<MyOtherClass> task1(){
return Task.Run(()=>{
Console.WriteLine("Task 1 Doing Something");
return new MyOtherClass();
});
}
And of course the aweosome...awesome:-) results:
Another thought is that you could implement your two different tasks as abstract methods, but that's a different conversation all together.
In addition to all of the great answers, I prefer to use Action Filter and ExpandoObject.
In Program File you should add your custom action Filter.
builder.Services.AddControllers(opt =>
{
opt.Filters.Add<ResponseHandler>();
});
and ResponseHandler acts like below:
public class ResponseHandler : IActionFilter
{
public void OnActionExecuted(ActionExecutedContext context)
{
IDictionary<string, object> expando = new ExpandoObject();
foreach (var propertyInfo in (context.Result as ObjectResult).Value.GetType().GetProperties())
{
var currentValue = propertyInfo.GetValue((context.Result as ObjectResult).Value);
expando.Add(propertyInfo.Name, currentValue);
}
dynamic result = expando as ExpandoObject;
result.ApiName = context.ActionDescriptor.RouteValues["action"].ToString();
context.Result = new ObjectResult(result);
}
public void OnActionExecuting(ActionExecutingContext context)
{
}
}
I'm trying to make a general "GET" in MONGODB.
But it does not convert error:
Cannot implicty convert type
My Service
namespace AdmissionAnonymousForm.Infrascructure.Services
{
public class BankService: IBankService
{
private readonly IMongoCollection<Bank> _bank;
public BankService(IConfiguration config)
{
var client = new MongoClient(config.GetConnectionString("AdmissionAnonymous"));
var database = client.GetDatabase("AdmissionAnonymous");
_bank = database.GetCollection<Bank>("Bank");
}
public Task<Bank> Get()
{
return _bank.Find(bank => true).ToListAsync();
}
}
}
My Model
namespace AdmissionAnonymousForm.Infrascructure.Services.Core
{
public interface IBankService
{
Task<Bank> Get();
Task<Bank> Get(Guid id);
Task<Bank> Post(Bank bank);
void Update(Guid id, Bank addressIn);
void Delete(Guid id);
}
}
the return type of Get method should be Task<List<Bank>>
public Task<List<Bank>> Get()
{
return _bank.Find(bank => true).ToListAsync();
}
You are returning a list and is expecting a single object of Bank
I am posting this question after reading the posts available. I have an ASP.NET web api controller with following methods.
[DataContract]
public class CustomPerson
{
[DataMember]
public ulong LongId { get; set; }
}
[DataContract]
public class Employee : CustomPerson
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string Address { get; set; }
}
Then in the controller
public class CustomController : ApiController
{
[HttpPost]
[ActionName("AddEmployee")]
public bool AddEmployee(Employee empInfo)
{
bool bIsSuccess = false;
// Code loginc here
bIsSuccess = true;
return bIsSuccess;
}
[HttpPost]
[ActionName("AddEmployeeCustom")]
public async Task<bool> AddEmployeeCustom()
{
string rawRequest = await Request.Content.ReadAsStringAsync();
bool bIsSuccess = false;
// Code loginc here
try
{
Employee emp = JsonConvert.DeserializeObject<Employee>(rawRequest);
}
catch (Exception ex)
{ }
return bIsSuccess;
}
}
When I call the following request to AddEmployee via soap ui the custom object is received without error i.e the empty value for LongId is ignored
{
"Name": "test1",
"Address": "Street 1",
"LongId": ""
}
When I call the AddEmployeeCustom method the runtime throws exception:
Error converting value "" to type 'System.UInt64'. Path 'LongId', line 4, position 14.
One option I read is to convert the incoming string to JObject and then create object of Employee class but I am trying to understand and mimic the behavior of default request handling mechanism when the incoming request is automatically handled by the controller and deserialized to Employee object
The problem is that your JSON is not valid for your model.
In the first method AddEmployee the process called Model Binding happens. MVC does the job of converting post content into the object. It seems its tolerant to type mismatch and forgives you the empty string.
In the second case, you try to do it yourself, and try simply run deserialization without validating input data. Newtonsoft JSON does not understand empty string and crashes.
If you still need to accept invalid JSON you may want to overwrite default deserialization process by implementing custom converter
public class NumberConverter : JsonConverter
{
public override bool CanWrite => false;
public override bool CanConvert(Type objectType)
{
return typeof(ulong) == objectType;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var value = reader.Value.ToString();
ulong result;
if (string.IsNullOrEmpty(value) || !ulong.TryParse(value, out result))
{
return default(ulong);
}
return result;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
And then specify your custom converter instance when calling deserialize
return JsonConvert.DeserializeObject<Employee>(doc.ToJson(), new NumberConverter());
This is my code:
// Controller
[HttpGet("{id}")]
[MyFilter]
public async Task<MyCustomType> Load(string id)
{
return new MyCustomType(....);
}
// Custom attribute
public class MyFilterAttribute : ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext context)
{
// Can I have my MyCustomType result here?
}
}
I need to implement some special logic in case of specific property values of MyCustomType result.
Public class MyCustomType
{
// assuming that there will be more properties
public int Id { get; set; }
public string Name { get; set; }
}
// Now, Move to Controller method.
public class CustomController : Controller
{
[HttpGet({"id"})]
[MyFilter]
public async Task<MyCustomType> Load(string id)
{
// Do some async operations
// Or do some Db queries
// returning MyCustomType
MyCustomType typeToReturn = new MyCustomType();
typeToReturn.Id = 1;
typeToReturn.Name = "something";
return typeToReturn;
}
}
// Well here goes the attribute
public class MyFilterAttribute : ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext context)
{
// you have to do more digging i am using dynamic to get the values and result.
dynamic content = context.Result;
if (content != null)
{
dynamic values = content.Value;
}
}
}
EDIT changed the code and ran it in a dot net core project and i was able to get the values, how ever i have used dynamic you can dig more on it.
I have a project where I want to use route attributes with a custom type.
The following code where I have the custom type as a query parameter works fine and the help page displays the custom type.
// GET api/values?5,6
[Route("api/values")]
public string Get(IntegerListParameter ids)
{
return "value";
}
WebApi.HelpPage gives the following documentation
Help:Page
If I change the code to use route attributes, the result is that I get an empty help page.
// GET api/values/5,6
[Route("api/values/{ids}")]
public string Get(IntegerListParameter ids)
{
return "value";
}
When I inspect the code I observe in HelpController.cs that ApiExplorer.ApiDescriptions returns an empty collection of ApiDescriptions
public ActionResult Index()
{
ViewBag.DocumentationProvider = Configuration.Services.GetDocumentationProvider();
Collection<ApiDescription> apiDescriptions = Configuration.Services.GetApiExplorer().ApiDescriptions;
return View(apiDescriptions);
}
Is there any way to get ApiExplorer to recognize my custom class IntegerListParameter as attribute routing?
You need to:
add HttpParameterBinding for your IntegerListParameter type
mark the binding as IValueProviderParameterBinding and implement ValueProviderFactories
add a converter for IntegerListParameter and override CanConvertFrom method for typeof(string) parameter
After these actions, route with custom type IntegerListParameter must be recognized in ApiExplorer.
See my example for type ObjectId:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
//...
config.ParameterBindingRules.Insert(0, GetCustomParameterBinding);
TypeDescriptor.AddAttributes(typeof(ObjectId), new TypeConverterAttribute(typeof(ObjectIdConverter)));
//...
}
public static HttpParameterBinding GetCustomParameterBinding(HttpParameterDescriptor descriptor)
{
if (descriptor.ParameterType == typeof(ObjectId))
{
return new ObjectIdParameterBinding(descriptor);
}
// any other types, let the default parameter binding handle
return null;
}
}
public class ObjectIdParameterBinding : HttpParameterBinding, IValueProviderParameterBinding
{
public ObjectIdParameterBinding(HttpParameterDescriptor desc)
: base(desc)
{
}
public override Task ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken)
{
try
{
SetValue(actionContext, new ObjectId(actionContext.ControllerContext.RouteData.Values[Descriptor.ParameterName] as string));
return Task.CompletedTask;
}
catch (FormatException)
{
throw new BadRequestException("Invalid id format");
}
}
public IEnumerable<ValueProviderFactory> ValueProviderFactories { get; } = new[] { new QueryStringValueProviderFactory() };
}
public class ObjectIdConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
return true;
return base.CanConvertFrom(context, sourceType);
}
}
Not exactly sure what data structure IntegerListParameter list is but if you need to send a comma delimited list of integers in the query(e.g. ~api/products?ids=1,2,3,4) you can use filter attributes. An example implementation of this can be found here: Convert custom action filter for Web API use?