I'm using Swashbuckle.Examples in Web API for better documentation. It is working fine for Swashbuckle Sample response but when I'm using Sample Example
When I run the project it is showing an Error.
My controller
[SwaggerResponse(HttpStatusCode.OK, Type = typeof(IEnumerable<ReasonReponsesuccessMessage_list>))]
[SwaggerResponseExample(HttpStatusCode.OK, typeof(IEnumerable<ReasonReponseSuccessExample_list>))]
[SwaggerResponse(HttpStatusCode.BadRequest, Type = typeof(IEnumerable<ReponseEmptyMessage>))]
[SwaggerOperation("List reasons")]
[ActionName("Reasons")]
[Route("api/{Id}")]
[HttpGet]
public HttpResponseMessage GetReasons(string Id)
{
}
Response Example Class
public class ReasonReponseSuccessExample_list : IExamplesProvider
{
object IExamplesProvider.GetExamples()
{
ReasonReponsesuccessMessage_list ReasonReponsesuccessMessage_list = new ReasonReponsesuccessMessage_list();
ReasonReponsesuccessMessage_list.Message = "Success";
ReasonReponsesuccessMessage_list.Data = new List<tbl_reason>
{
new tbl_reason{ id="SAA133",primary_name="Wrong Invoice",alt_name="Wrong Invoice"},
new tbl_reason{ id="B97123",primary_name="Payment Problem",alt_name=""}
};
ReasonReponsesuccessMessage_list.Extras = "";
ReasonReponsesuccessMessage_list.Success = true;
return ReasonReponsesuccessMessage_list;
}
}
ERROR:
Expected examplesProviderType to implement
Swashbuckle.Examples.IExamplesProvider.
System.Collections.Generic.IEnumerable`1[IgniteAPI.Payload.ReasonReponseSuccessExample_list]
does not.
I'm Getting this error in global.asmx
GlobalConfiguration.Configure(WebApiConfig.Register);
As you can see in the error, you need to specify type that implements
IExamplesProvider
Use
[SwaggerResponseExample(HttpStatusCode.OK, typeof(ReasonReponseSuccessExample_list))]
instead of
[SwaggerResponseExample(HttpStatusCode.OK, typeof(IEnumerable<ReasonReponseSuccessExample_list>))]
I'm trying to write some unit tests for my ASP.NET MVC code, but I'm hitting a road block.
I have entity partial classes (in a business class library assembly) that need to determine a URL to call for an action method and controller. In order to do that, I found this snippet of code which works nicely - but alas, this uses the HttpContext.Current and thus prevents me from writing any unit tests:
public string NavigateUrl
{
get
{
HttpContextWrapper httpContextWrapper = new HttpContextWrapper(HttpContext.Current);
UrlHelper urlHelper = new UrlHelper(new RequestContext(httpContextWrapper, RouteTable.Routes.GetRouteData(httpContextWrapper)));
string url = urlHelper.Action("SomeAction", "MyController");
}
}
I am reading about the HttpContextBase - but how does this come into play here?? Or is there another way to determine an action URL inside an entity class (that is in a business assembly - NOT the MVC project and not inside a controller or other MVC infrastructure class)?
Update: I need to return this URL from an entity class as a string, since I need to use it in a grid as the navigation URL of a hyperlink. And in reality, there are numerous conditions being checked and the URL string returned can be one of several possibilities - so I cannot just replace it by a single controller call...
Create an abstraction to represent the desired functionality.
For example
public interface IUrlHelper {
string Action(string actionName, string controllerName);
//TODO: create other desired members to be exposed
}
You then create a factory for that abstraction. Since you are not injecting it into the entities we are using a service locator ani-pattern.
public static class UrlHelperFactory {
public static Func<IUrlHelper> Create = () => {
throw new NotImplementedException();
};
}
The helper and factory are not coupled to anything and could live anywhere in the solution.
The following test mocks the service to allow the entity to be tested in isolation.
[TestClass]
public class UrlHelperFactory_Should {
public class MyTestEntity {
public string NavigateUrl {
get {
var urlHelper = UrlHelperFactory.Create();
string url = urlHelper.Action("SomeAction", "MyController");
return url;
}
}
}
[TestMethod]
public void Generate_NavigationUrl() {
//Arrange
var mockHelper = Mock.Of<IUrlHelper>();
UrlHelperFactory.Create = () => {
return mockHelper;
};
var expected = "http://my_fake_url";
Mock.Get(mockHelper)
.Setup(_ => _.Action(It.IsAny<string>(), It.IsAny<string>()))
.Returns(expected);
var sut = new MyTestEntity();
//Act
var actual = sut.NavigateUrl;
//Assert
actual.Should().NotBeNullOrWhiteSpace()
.And.Be(expected);
}
}
In production code at the composition root you make sure that the factory knows how to build the service
UrlHelperFactory.Create = () => {
var httpContextWrapper = new HttpContextWrapper(HttpContext.Current);
var urlHelper = new UrlHelper(new RequestContext(httpContextWrapper, RouteTable.Routes.GetRouteData(httpContextWrapper)));
return new DefaultUrlHelperWrapper(urlHelper);
};
Where a wrapper could look like this...
internal class DefaultUrlHelperWrapper : IUrlHelper {
private UrlHelper urlHelper;
public DefaultUrlHelperWrapper(UrlHelper urlHelper) {
this.urlHelper = urlHelper;
}
public string Action(string actionName, string controllerName) {
return urlHelper.Action(actionName, controllerName);
}
//TODO: Implement other members
}
I have a function in a controller that I am unit testing that expects values in the header of the http request. I can't initialize the HttpContext because it is readonly.
My controller function expects a http request header value for "device-id"
[TestMethod]
public void TestValuesController()
{
ValuesController controller = new ValuesController();
//not valid controller.HttpContext is readonly
//controller.HttpContext = new DefaultHttpContext();
var result = controller.Get();
Assert.AreEqual(result.Count(), 2);
}
Is there a straight-forward way to do this without using a third party library?
I was able to initialize the httpcontext and header in this way:
[TestMethod]
public void TestValuesController()
{
ValuesController controller = new ValuesController();
controller.ControllerContext = new ControllerContext();
controller.ControllerContext.HttpContext = new DefaultHttpContext();
controller.ControllerContext.HttpContext.Request.Headers["device-id"] = "20317";
var result = controller.Get();
//the controller correctly receives the http header key value pair device-id:20317
...
}
Rather than mocking out the HTTPContext, it is probably a better idea to map the header into a parameter on the method. For example, in the controller at the bottom of this answer, the id parameter is set to the value header with a name equal to "device-id"... The unit test then becomes
[TestMethod]
public void TestValuesController()
{
ValuesController controller = new ValuesController();
var result = controller.GetHeaderValue("27");
Assert.AreEqual(result, "27");
}
While you can mock the HttpContext, in my opinion it is something that should be avoided unless you have no choice. The documentation for the FromHeaderAttribute can be found here FromHeaderAttribute Class.
public class ValuesController: Controller
{
public string GetHeaderValue([FromHeader(Name = "device-id")] string id)
{
return id;
}
}
For people in need of a header but also additional data in their HttpContext, you can do so by initializing the context with features thanks to the second constructor of the DefaultHttpContext class:
1. Create a header dictionary with the headers you need:
var headers = new Dictionary<string, StringValues>
{
{ "myHeaderKey", "myHeaderValue" },
};
var headerDictionary = new HeaderDictionary(headers)
2. Create an HttpRequestFeature with the previously created header dictionary:
var requestFeature = new HttpRequestFeature()
{
Headers = headerDictionary,
};
3. Create a Feature collection containing the feature previously created :
var features = new FeatureCollection();
features.Set<IHttpRequestFeature>(requestFeature);
4. Initialize the DefaultHttpContext with the feature collection, and set it as the HttpContext of your controller:
var httpContext = new DefaultHttpContext(features);
var controller = new MyController();
controller.ControllerContext = new ControllerContext();
controller.ControllerContext.HttpContext = httpContext;
The controller's context will have the correct headers set, and you can still feed the context with more data as needed by setting additional HttpContext attributes to the featureCollection before instantiating the DefaultHttpContext (like feature.Set<IQueryFeature>(new QueryFeature(...)) for the query string for instance).
PS: For a more in-depth explanation on using features to mock (and unit testing in general) an HttpContext, see: https://weblogs.asp.net/ricardoperes/unit-testing-the-httpcontext-in-controllers
I'm struggling to add OData v4 query support to a method on a controller that inherits from ApiController rather than ODataController. While I have a working OData model in the solution there are some endpoints that don't really belong in the model but the power of the query would be useful.
I've seen some articles suggesting I can just return an IQueryable and use EnableQuery.
Here is my sample code:
public class TestController : ApiController
{
[HttpGet]
[EnableQuery]
public IQueryable<TestObject> Events()
{
var result = new[] { new TestObject { Id = "1" } }.AsQueryable();
return result;
}
}
public class TestObject
{
public string Id { get; set; }
}
All I get back when a call /Test/Events is a 406 Not Acceptable, something that I've run into a lot dealing with OData that usually means I've return something the OData framework doesn't like. I've never been able to get anymore info out of the framework (a major failing I think) as to why it doesn't like it and only solved them by trial and error in the past.
Has anyone got this setup working and if so how?
Or any suggestions on debugging the 406 response?
EDIT:
OK so it turned out the culprit was so code in the start up that was registering a custom ODataMediaTypeFormatter and in the process was clearing all other formatters.
After removing the offending code it worked.
Really really wish WebApi would record somewhere what caused the 406 error when it spits one out.
You can apply the "query" magic manually, without [EnableQuery].
Create a ODataQueryContext, passing in your model (you'll need to
save it in global and make it available), and the entity type your are
querying against.
Create an HttpRequestMessage using Get and the current url
(Request.RequestUri.AbsoluteUri).
Create a new ODataQueryOptions<yourEntity>, passing in the context and
request message you created.
Call that object's ApplyTo using your entity.AsQueryable(), casting
the result as an IQueryable<yourEntity>.
Further manipulate needed and return. Keep in mind that return type does not need to be IQueryable<yourEntity>, or even based upon "yourEntity".
Here is a similar implementation that does this for an ODataController method ( as a base ODataQueryOptions comes for free):
public IEnumerable<Aggregate> GetOrders(ODataQueryOptions<ReportingOrder> opts, [FromUri(Name="$groupby")]string groupby, [FromUri(Name="$aggregates")]string aggregates = null)
{
var url = opts.Request.RequestUri.AbsoluteUri;
int? top = null;
if (opts.Top != null)
{
top = int.Parse(opts.Top.RawValue);
var topStr = string.Format("$top={0}", top.Value);
url = url.Replace(topStr, "");
var req = new HttpRequestMessage(HttpMethod.Get, url);
opts = new ODataQueryOptions<ReportingOrder>(opts.Context, req);
}
var query = opts.ApplyTo(db.ReportingOrders.AsQueryable()) as IQueryable<ReportingOrder>;
var results = query.GroupBy(groupby, aggregates, top);
return results;
}
Can some please help me to know how to pass multiple objects from a C# console app to Web API controller as shown below?
using (var httpClient = new System.Net.Http.HttpClient())
{
httpClient.BaseAddress = new Uri(ConfigurationManager.AppSettings["Url"]);
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = httpClient.PutAsync("api/process/StartProcessiong", objectA, objectB);
}
My Web API method is like this:
public void StartProcessiong([FromBody]Content content, [FromBody]Config config)
{
}
In the current version of Web API, the usage of multiple complex objects (like your Content and Config complex objects) within the Web API method signature is not allowed. I'm betting good money that config (your second parameter) is always coming back as NULL. This is because only one complex object can be parsed from the body for one request. For performance reasons, the Web API request body is only allowed to be accessed and parsed once. So after the scan and parsing occurs of the request body for the "content" parameter, all subsequent body parses will end in "NULL". So basically:
Only one item can be attributed with [FromBody].
Any number of items can be attributed with [FromUri].
Below is a useful extract from Mike Stall's excellent blog article (oldie but goldie!). You'll want to pay attention to item 4:
Here are the basic rules to determine whether a parameter is read with model binding or a formatter:
If the parameter has no attribute on it, then the decision is made purely on the parameter's .NET type. "Simple types" use model binding. Complex types use the formatters. A "simple type" includes: primitives, TimeSpan, DateTime, Guid, Decimal, String, or something with a TypeConverter that converts from strings.
You can use a [FromBody] attribute to specify that a parameter should be from the body.
You can use a [ModelBinder] attribute on the parameter or the parameter's type to specify that a parameter should be model bound. This attribute also lets you configure the model binder. [FromUri] is a derived instance of [ModelBinder] that specifically configures a model binder to only look in the URI.
The body can only be read once. So if you have 2 complex types in the signature, at least one of them must have a [ModelBinder] attribute on it.
It was a key design goal for these rules to be static and predictable.
A key difference between MVC and Web API is that MVC buffers the content (e.g. request body). This means that MVC's parameter binding can repeatedly search through the body to look for pieces of the parameters. Whereas in Web API, the request body (an HttpContent) may be a read-only, infinite, non-buffered, non-rewindable stream.
You can read the rest of this incredibly useful article on your own so, to cut a long story short, what you're trying to do is not currently possible in that way (meaning, you have to get creative). What follows is not a solution, but a workaround and only one possibility; there are other ways.
Solution/Workaround
(Disclaimer: I've not used it myself, I'm just aware of the theory!)
One possible "solution" is to use the JObject object. This objects provides a concrete type specifically designed for working with JSON.
You simply need to adjust the signature to accept just one complex object from the body, the JObject, let's call it stuff. Then, you manually need to parse properties of the JSON object and use generics to hydrate the concrete types.
For example, below is a quick'n'dirty example to give you an idea:
public void StartProcessiong([FromBody]JObject stuff)
{
// Extract your concrete objects from the json object.
var content = stuff["content"].ToObject<Content>();
var config = stuff["config"].ToObject<Config>();
. . . // Now do your thing!
}
I did say there are other ways, for example you can simply wrap your two objects in a super-object of your own creation and pass that to your action method. Or you can simply eliminate the need for two complex parameters in the request body by supplying one of them in the URI. Or ... well, you get the point.
Let me just reiterate I've not tried any of this myself, although it should all work in theory.
As #djikay mentioned, you cannot pass multiple FromBody parameters.
One workaround I have is to define a CompositeObject,
public class CompositeObject
{
public Content Content { get; set; }
public Config Config { get; set; }
}
and have your WebAPI takes this CompositeObject as the parameter instead.
public void StartProcessiong([FromBody] CompositeObject composite)
{ ... }
You could try posting multipart content from the client like this:
using (var httpClient = new HttpClient())
{
var uri = new Uri("http://example.com/api/controller"));
using (var formData = new MultipartFormDataContent())
{
//add content to form data
formData.Add(new StringContent(JsonConvert.SerializeObject(content)), "Content");
//add config to form data
formData.Add(new StringContent(JsonConvert.SerializeObject(config)), "Config");
var response = httpClient.PostAsync(uri, formData);
response.Wait();
if (!response.Result.IsSuccessStatusCode)
{
//error handling code goes here
}
}
}
On the server side you could read the the content like this:
public async Task<HttpResponseMessage> Post()
{
//make sure the post we have contains multi-part data
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
//read data
var provider = new MultipartMemoryStreamProvider();
await Request.Content.ReadAsMultipartAsync(provider);
//declare backup file summary and file data vars
var content = new Content();
var config = new Config();
//iterate over contents to get Content and Config
foreach (var requestContents in provider.Contents)
{
if (requestContents.Headers.ContentDisposition.Name == "Content")
{
content = JsonConvert.DeserializeObject<Content>(requestContents.ReadAsStringAsync().Result);
}
else if (requestContents.Headers.ContentDisposition.Name == "Config")
{
config = JsonConvert.DeserializeObject<Config>(requestContents.ReadAsStringAsync().Result);
}
}
//do something here with the content and config and set success flag
var success = true;
//indicate to caller if this was successful
HttpResponseMessage result = Request.CreateResponse(success ? HttpStatusCode.OK : HttpStatusCode.InternalServerError, success);
return result;
}
}
I know this is an old question, but I had the same issue and here is what I came up with and hopefully will be useful to someone. This will allow passing JSON formatted parameters individually in request URL (GET), as one single JSON object after ? (GET) or within single JSON body object (POST). My goal was RPC-style functionality.
Created a custom attribute and parameter binding, inheriting from HttpParameterBinding:
public class JSONParamBindingAttribute : Attribute
{
}
public class JSONParamBinding : HttpParameterBinding
{
private static JsonSerializer _serializer = JsonSerializer.Create(new JsonSerializerSettings()
{
DateTimeZoneHandling = DateTimeZoneHandling.Utc
});
public JSONParamBinding(HttpParameterDescriptor descriptor)
: base(descriptor)
{
}
public override Task ExecuteBindingAsync(ModelMetadataProvider metadataProvider,
HttpActionContext actionContext,
CancellationToken cancellationToken)
{
JObject jobj = GetJSONParameters(actionContext.Request);
object value = null;
JToken jTokenVal = null;
if (!jobj.TryGetValue(Descriptor.ParameterName, out jTokenVal))
{
if (Descriptor.IsOptional)
value = Descriptor.DefaultValue;
else
throw new MissingFieldException("Missing parameter : " + Descriptor.ParameterName);
}
else
{
try
{
value = jTokenVal.ToObject(Descriptor.ParameterType, _serializer);
}
catch (Newtonsoft.Json.JsonException e)
{
throw new HttpParseException(String.Join("", "Unable to parse parameter: ", Descriptor.ParameterName, ". Type: ", Descriptor.ParameterType.ToString()));
}
}
// Set the binding result here
SetValue(actionContext, value);
// now, we can return a completed task with no result
TaskCompletionSource<AsyncVoid> tcs = new TaskCompletionSource<AsyncVoid>();
tcs.SetResult(default(AsyncVoid));
return tcs.Task;
}
public static HttpParameterBinding HookupParameterBinding(HttpParameterDescriptor descriptor)
{
if (descriptor.ActionDescriptor.ControllerDescriptor.GetCustomAttributes<JSONParamBindingAttribute>().Count == 0
&& descriptor.ActionDescriptor.GetCustomAttributes<JSONParamBindingAttribute>().Count == 0)
return null;
var supportedMethods = descriptor.ActionDescriptor.SupportedHttpMethods;
if (supportedMethods.Contains(HttpMethod.Post) || supportedMethods.Contains(HttpMethod.Get))
{
return new JSONParamBinding(descriptor);
}
return null;
}
private JObject GetJSONParameters(HttpRequestMessage request)
{
JObject jobj = null;
object result = null;
if (!request.Properties.TryGetValue("ParamsJSObject", out result))
{
if (request.Method == HttpMethod.Post)
{
jobj = JObject.Parse(request.Content.ReadAsStringAsync().Result);
}
else if (request.RequestUri.Query.StartsWith("?%7B"))
{
jobj = JObject.Parse(HttpUtility.UrlDecode(request.RequestUri.Query).TrimStart('?'));
}
else
{
jobj = new JObject();
foreach (var kvp in request.GetQueryNameValuePairs())
{
jobj.Add(kvp.Key, JToken.Parse(kvp.Value));
}
}
request.Properties.Add("ParamsJSObject", jobj);
}
else
{
jobj = (JObject)result;
}
return jobj;
}
private struct AsyncVoid
{
}
}
Inject binding rule inside WebApiConfig.cs's Register method:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.ParameterBindingRules.Insert(0, JSONParamBinding.HookupParameterBinding);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
This allows for controller actions with default parameter values and mixed complexity, as such:
[JSONParamBinding]
[HttpPost, HttpGet]
public Widget DoWidgetStuff(Widget widget, int stockCount, string comment="no comment")
{
... do stuff, return Widget object
}
example post body:
{
"widget": {
"a": 1,
"b": "string",
"c": { "other": "things" }
},
"stockCount": 42,
"comment": "sample code"
}
or GET single param (needs URL encoding)
controllerPath/DoWidgetStuff?{"widget":{..},"comment":"test","stockCount":42}
or GET multiple param (needs URL encoding)
controllerPath/DoWidgetStuff?widget={..}&comment="test"&stockCount=42
Create one complex object to combine Content and Config in it as others mentioned, use dynamic and just do a .ToObject(); as:
[HttpPost]
public void StartProcessiong([FromBody] dynamic obj)
{
var complexObj= obj.ToObject<ComplexObj>();
var content = complexObj.Content;
var config = complexObj.Config;
}
Best way to pass multiple complex object to webapi services is by using tuple other than dynamic, json string, custom class.
HttpClient.PostAsJsonAsync("http://Server/WebService/Controller/ServiceMethod?number=" + number + "&name" + name, Tuple.Create(args1, args2, args3, args4));
[HttpPost]
[Route("ServiceMethod")]
[ResponseType(typeof(void))]
public IHttpActionResult ServiceMethod(int number, string name, Tuple<Class1, Class2, Class3, Class4> args)
{
Class1 c1 = (Class1)args.Item1;
Class2 c2 = (Class2)args.Item2;
Class3 c3 = (Class3)args.Item3;
Class4 c4 = (Class4)args.Item4;
/* do your actions */
return Ok();
}
No need to serialize and deserialize passing object while using tuple.
If you want to send more than seven complex object create internal tuple object for last tuple argument.
Here's another pattern that may be useful to you. It's for a Get but the same principle and code applies for a Post/Put but in reverse. It essentially works on the principle of converting objects down to this ObjectWrapper class which persists the Type's name to the other side:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
namespace WebAPI
{
public class ObjectWrapper
{
#region Public Properties
public string RecordJson { get; set; }
public string TypeFullName { get; set; }
#endregion
#region Constructors
public ObjectWrapper() : this(null, null)
{
}
public ObjectWrapper(object objectForWrapping) : this(objectForWrapping, null)
{
}
public ObjectWrapper(object objectForWrapping, string typeFullName)
{
if (typeFullName == null && objectForWrapping != null)
{
TypeFullName = objectForWrapping.GetType().FullName;
}
else
{
TypeFullName = typeFullName;
}
RecordJson = JsonConvert.SerializeObject(objectForWrapping);
}
#endregion
#region Public Methods
public object ToObject()
{
var type = Type.GetType(TypeFullName);
return JsonConvert.DeserializeObject(RecordJson, type);
}
#endregion
#region Public Static Methods
public static List<ObjectWrapper> WrapObjects(List<object> records)
{
var retVal = new List<ObjectWrapper>();
records.ForEach
(item =>
{
retVal.Add
(
new ObjectWrapper(item)
);
}
);
return retVal;
}
public static List<object> UnwrapObjects(IEnumerable<ObjectWrapper> objectWrappers)
{
var retVal = new List<object>();
foreach(var item in objectWrappers)
{
retVal.Add
(
item.ToObject()
);
}
return retVal;
}
#endregion
}
}
In the REST code:
[HttpGet]
public IEnumerable<ObjectWrapper> Get()
{
var records = new List<object>();
records.Add(new TestRecord1());
records.Add(new TestRecord2());
var wrappedObjects = ObjectWrapper.WrapObjects(records);
return wrappedObjects;
}
This is the code on the client side (UWP) using a REST client library. The client library just uses the Newtonsoft Json serialization library - nothing fancy.
private static async Task<List<object>> Getobjects()
{
var result = await REST.Get<List<ObjectWrapper>>("http://localhost:50623/api/values");
var wrappedObjects = (IEnumerable<ObjectWrapper>) result.Data;
var unwrappedObjects = ObjectWrapper.UnwrapObjects(wrappedObjects);
return unwrappedObjects;
}
Basically you can send complex object without doing any extra fancy thing. Or without making changes to Web-Api. I mean why would we have to make changes to Web-Api, while the fault is in our code that's calling the Web-Api.
All you have to do use NewtonSoft's Json library as following.
string jsonObjectA = JsonConvert.SerializeObject(objectA);
string jsonObjectB = JsonConvert.SerializeObject(objectB);
string jSoNToPost = string.Format("\"content\": {0},\"config\":\"{1}\"",jsonObjectA , jsonObjectB );
//wrap it around in object container notation
jSoNToPost = string.Concat("{", jSoNToPost , "}");
//convert it to JSON acceptible content
HttpContent content = new StringContent(jSoNToPost , Encoding.UTF8, "application/json");
var response = httpClient.PutAsync("api/process/StartProcessiong", content);
Here I found a workaround to pass multiple generic objects (as json) from jquery to a WEB API using JObject, and then cast back to your required specific object type in api controller. This objects provides a concrete type specifically designed for working with JSON.
var combinedObj = {};
combinedObj["obj1"] = [your json object 1];
combinedObj["obj2"] = [your json object 2];
$http({
method: 'POST',
url: 'api/PostGenericObjects/',
data: JSON.stringify(combinedObj)
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
alert("Saved Successfully !!!");
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
alert("Error : " + response.data.ExceptionMessage);
});
and then you can get this object in your controller
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public [OBJECT] PostGenericObjects(object obj)
{
string[] str = GeneralMethods.UnWrapObjects(obj);
var item1 = JsonConvert.DeserializeObject<ObjectType1>(str[0]);
var item2 = JsonConvert.DeserializeObject<ObjectType2>(str[1]);
return *something*;
}
I have made a generic function to unwrap the complex object, so there is no limitation of number of objects while sending and unwrapping. We can even send more than two objects
public class GeneralMethods
{
public static string[] UnWrapObjects(object obj)
{
JObject o = JObject.Parse(obj.ToString());
string[] str = new string[o.Count];
for (int i = 0; i < o.Count; i++)
{
string var = "obj" + (i + 1).ToString();
str[i] = o[var].ToString();
}
return str;
}
}
I have posted the solution to my blog with a little more description with simpler code to integrate easily.
Pass multiple complex objects to Web API
I hope it would help someone. I would be interested to hear from the experts here regarding the pros and cons of using this methodology.
Late answer, but you can take advantage of the fact that you can deserialize multiple objects from one JSON string, as long as the objects don't share any common property names,
public async Task<HttpResponseMessage> Post(HttpRequestMessage request)
{
var jsonString = await request.Content.ReadAsStringAsync();
var content = JsonConvert.DeserializeObject<Content >(jsonString);
var config = JsonConvert.DeserializeObject<Config>(jsonString);
}
Create a Composite object
public class CollectiveObject<X, Y>
{
public X FirstObj;
public Y SecondObj;
}
initialize a composite object with any two objects which you willing to send.
CollectiveObject<myobject1, myobject2> collectiveobj =
new CollectiveObject<myobject1, myobject2>();
collectiveobj.FirstObj = myobj1;
collectiveobj.SecondObj = myobj2;
Do serialization
var req = JSONHelper.JsonSerializer`<CollectiveObject<myobject1, `myobject2>>(collectiveobj);`
`
your API must be like
[Route("Add")]
public List<APIAvailibilityDetails> Add([FromBody]CollectiveObject<myobject1, myobject2> collectiveobj)
{ //to do}