Asp.Net Core, [FromQuery] complex class - c#

I have the followings classes:
public class TextFilter
{
public string Value { get; set; } = "";
public EnumTextFilterSearchMethod SearchMethod { get; set; } = EnumTextFilterSearchMethod.EQ;
}
public class ContractsFilter
{
public TextFilter ContractNumber { get; set; } = new TextFilter();
public TextFilter OrderNumber { get; set; } = new TextFilter();
}
and controller:
[HttpGet("")]
public IActionResult Contracts([FromQuery] ContractsFilter filter = null)
the query string appears as:
http://localhost:63553/contracts?ContractNumber=my.namespace.TextFilter&OrderNumber=my.namespace.TextFilter
But I would need that the query string is formed as following:
http://localhost:63553/contracts?ContractNumber.Value=any_contract_number&ContractNumber.SearchMethod=EQ&OrderNumber.Value=any_order_number&OrderNumber.SearchMethod=EQ
If I enter so the query string by hand, the controller can it parse properly.
How can I make the class ContractsFilter appears in the query string with all properties of class TextFilter?

Try the [FromUri] attribute before your in-parameter in the same way you’ve tried with the [FromBody] attribute.

Related

Map jQuery datatable search param to property of class in c#

I am using .net core clean architecture along with jQuery datatable. Server-side search is enabled, but I cannot map that search param search[value] from datable to a model property in c#. I have tried the Newtonsoft JsonPropertyName attribute to map it but it fails. Below is my model code:
public class GetVotesByMeetingIdQuery : IRequest<PaginatedList<VoteCastDTO>>
{
public int PageNumber { get; set; } = 1;
public int PageSize { get; set; } = 10;
public Search Search { get; set; }
}
public class Search
{
[JsonProperty(PropertyName = "value")]
public string Value { set; get; }
[JsonProperty(PropertyName = "regex")]
public bool Regex { set; get; }
}
I am able to capture the param from the request in my controller.
[HttpGet("GetVotesByMeetingId")]
public async Task<ActionResult<PaginatedList<VoteCastDTO>>> GetVotesByMeetingId([FromQuery] GetVotesByMeetingIdQuery query)
{
var exist = Request.Query.TryGetValue("search[value]", out Microsoft.Extensions.Primitives.StringValues val);
query.Search = exist ? val.ToString() : string.Empty;
return await Mediator.Send(query);
}
but I don't want to do this as I want to keep my controller clean. Is there anyway to sort out this issue?
You can use [FromQuery] attribute on the property. It will map the parameter to the property accordingly. Also, you need to change the property type to string as you are getting the param value in string. Below is the example:
public class GetVotesByMeetingIdQuery : IRequest<PaginatedList<VoteCastDTO>>
{
public int PageNumber { get; set; } = 1;
public int PageSize { get; set; } = 10;
[FromQuery(Name = "search[value]")]
public string Search { get; set; }
}

Why the controller response are setting model field names into lower case?

In my .NET Core project, in the response of all controllers, the object fields are coming in lower case in the first one or two letters of the field name:
{
"iD_PARAM": "foo",
"cD_PROM": "bar",
"txT_OFFICER": "lorem",
"cN_NEW_PARAM": "fubá",
"iD_SITUATION": "XX",
"iD_NEW_USER": "ipsun",
}
It's strange, because the model has all fields in UPPER case:
public partial class MyModel {
public long ID_PARAM { get; set; }
public long CD_PROM { get; set; }
public string TXT_OFFICER { get; set; }
public int CN_NEW_PARAM { get; set; }
public int ID_SITUATION { get; set; }
public int ID_NEW_USER { get; set; }
}
For more detail, this is the controller where I set the values and the response:
[HttpPost("receive")]
public async Task<IActionResult> Get()
{
try
{
MyModel newParam = new MyModel ();
newParam.ID_PARAM = "foo";
newParam.CD_PROM = "foo";
newParam.TXT_OFFICER = "lorem";
newParam.CN_NEW_PARAM = "fubá";
newParam.ID_SITUATION = "XX";
newParam.ID_NEW_USER = "ipsun";
return Ok(newParam);
}
catch (Exception ex)
{
return BadRequest(ex);
}
}
Assuming you are using Newtonsoft Json, if you want your Json properties to be uppercase, try decorating your Model with JsonProperty like this to prevent the Serializer try to infer the property name :
public partial class MyModel {
[JsonProperty("ID_PARAM")]
public long ID_PARAM { get; set; }
[JsonProperty("CD_PROM")]
public long CD_PROM { get; set; }
[JsonProperty("TXT_OFFICER")]
public string TXT_OFFICER { get; set; }
[JsonProperty("CN_NEW_PARAM")]
public int CN_NEW_PARAM { get; set; }
[JsonProperty("ID_SITUATION")]
public int ID_SITUATION { get; set; }
[JsonProperty("ID_NEW_USER")]
public int ID_NEW_USER { get; set; }
}
You should change the ContractResolver, just add below code in startup ConfigurSservices
services.AddMvc().AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
});
Refer to Lowercase property names from Json() in .Net core

failing to bind string indexed query string parameters

I'm attempting to bind some query string parameters that is indexed by string keys but i can't seem to be getting it to work
here are the values i was trying to bind
search[value]: Exception happ...
search[regex]: false
here is the model i'm trying to bind it with
getLogsAjax(DataTableAjaxPostModel model)
public class DataTableAjaxPostModel
{
public int draw { get; set; }
public int start { get; set; }
public int length { get; set; }
public List<Column> columns { get; set; }
public search search { get; set; }
public List<Order> order { get; set; }
}
public class search
{
public string value { get; set; }
public string regex { get; set; }
}
the rest of the model is being bind correctly except for the search class object, i tripled check that the request contains values for that object, what am i missing here?
p.s. the same code was supposedly working pre .net core
A little more background of the code would be helpful such as the code section that is actually doing the binding however here is a dotnetcore controller example with query parameter binding. Also common practice in C# are class names and fields are both uppercase FYI.
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
[Route("api/[controller]")]
public class SampleController : Controller
{
[HttpGet]
[Route("")]
public IActionResult ExampleGet([FromQuery] DataTableAjaxPostModel dataTableAjaxPostModel)
{
// You should be able to debug and see the value here
var result = dataTableAjaxPostModel.search;
return Ok();
}
public class DataTableAjaxPostModel
{
public int draw { get; set; }
public int start { get; set; }
public int length { get; set; }
public List<Column> columns { get; set; }
public search search { get; set; }
public List<Order> order { get; set; }
}
public class search
{
public string value { get; set; }
public string regex { get; set; }
}
}
You don't need to bind each field manually. Using reflection will make it easily.
Aslo, there's no need bind those outer model's properties (DataTableAjaxPostModel's properties) manually. That's because they will be done by the built-in model binder.
Implementation
create a custom binder QueryStringDictSyntaxBinder<TModel>:
internal class QueryStringDictSyntaxBinder<TModel> : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null)
throw new ArgumentNullException(nameof(bindingContext));
try
{
var result = Activator.CreateInstance<TModel>();
foreach(var pi in typeof(TModel).GetProperties())
{
var modelName = bindingContext.ModelName;
var qsFieldName = $"{modelName}[{pi.Name}]";
var field= bindingContext.HttpContext.Request.Query[qsFieldName].FirstOrDefault();
if(field != null){
pi.SetValue(result,field);
}
// do nothing if null , or add model binding failure messages if you like
}
bindingContext.Result = ModelBindingResult.Success(result);
}
catch
{
bindingContext.Result = ModelBindingResult.Failed();
}
return Task.CompletedTask;
}
}
And then decorate the search property with a [ModelBinder(typeof(QueryStringDictSyntaxBinder<search>))] :
public class DataTableAjaxPostModel
{
public int draw { get; set; }
public int start { get; set; }
public int length { get; set; }
public List columns { get; set; }
[ModelBinder(typeof(QueryStringDictSyntaxBinder<search>))]
public search search { get; set; }
public List order { get; set; }
}
Test Case:
I test it with the following requests, and it works fine for me:
?draw=1&search[value]=abc&search[regex]=(.*)&
?draw=1&sEarCh[value]=opq&Search[regex]=([^123]*)&
?draw=1&seaRch[value]=rst&Search[regex]=(.*)&
?draw=1&Search[value]=abc&
?draw=1&
seems like no one has an answer for this, so i took a different route and wrote my own custom binder, if a better answer came, ill accept it instead of this one, probably will refactor it later (hahaha IKR!)
public class DTModelBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null)
throw new ArgumentNullException(nameof(bindingContext));
try
{
var result = new DataTableAjaxPostModel();
if (bindingContext.HttpContext.Request.Query.Keys.Contains("draw"))
result.draw = int.Parse(bindingContext.ValueProvider.GetValue("draw").FirstValue);
if (bindingContext.HttpContext.Request.Query.Keys.Contains("search[value]") &&
bindingContext.HttpContext.Request.Query.Keys.Contains("search[regex]"))
result.search = new search()
{
regex = bindingContext.ValueProvider.GetValue("search[regex]").FirstValue,
value = bindingContext.ValueProvider.GetValue("search[value]").FirstValue
};
//...
bindingContext.Result = ModelBindingResult.Success(result);
}
catch
{
bindingContext.Result = ModelBindingResult.Failed();
}
return Task.CompletedTask;
}
}

How to pass parameters to array class in webapi?

I am new to asp.net mvc webapi.I am create one webapi service.In this service I am sending parameter as an array class.
Below is my service :
[AcceptVerbs("GET", "POST")]
public HttpResponseMessage addBusOrder(string UserUniqueID, int PlatFormID,
string DeviceID, int RouteScheduleId,
string JourneyDate, int FromCityid,
int ToCityid, int TyPickUpID,
Contactinfo Contactinfo, passenger[] pass)
{
//done some work here
}
public class Contactinfo
{
public string Name { get; set; }
public string Email { get; set; }
public string Phoneno { get; set; }
public string mobile { get; set; }
}
public class passenger
{
public string passengerName { get; set; }
public string Age { get; set; }
public string Fare { get; set; }
public string Gender { get; set; }
public string Seatno { get; set; }
//public string Seattype { get; set; }
// public bool Isacseat { get; set; }
}
Now how to pass passenger and contactinfo parameters to the above service.
Is there any changes in webapiconfig file?
i want to pass passenger details like this:
passengername="pavan",
age="23",
Gender="M",
passengername="kumar",
Gender="M",
Age="22
It will be much neater if you can create model of your parameter. To pass them from client side, you need to format them using one of data-interchange format. I prefer use JSON provided by Newtonsoft.Json library. Sending process is handled by HttpClient class provided by System.Net.Http namespace. Here is some sample:
Server Side
//Only request with Post Verb that can contain body
[AcceptVerbs("POST")]
public HttpResponseMessage addBusOrder([FromBody]BusOrderModel)
{
//done some work here
}
//You may want to separate model into a class library so that server and client app can share the same model
public class BusOrderModel
{
public string UserUniqueID { get; set; }
public int PlatFormID { get; set; }
public string DeviceID { get; set; }
public int RouteScheduleId { get; set; }
public string JourneyDate { get; set; }
public int FromCityid { get; set; }
public int ToCityid { get; set; }
public int TyPickUpID { get; set; }
public Contactinfo ContactInfo { get; set; }
public passenger[] pass { get; set; }
}
Client Side
var busOrderModel = new BusOrderModel();
var content = new StringContent(JsonConvert.SerializeObject(busOrderModel), Encoding.UTF8, "application/json");
using (var handler = new HttpClientHandler())
{
using (HttpClient client = new HttpClient(handler, true))
{
client.BaseAddress = new Uri("yourdomain");
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
return await client.PostAsync(new Uri("yourdomain/controller/addBusOrder"), content);
}
}
Here's how you can do it:
First, since you are passing two objects as parameters we'll need a new class to hold them (because we can only bind one parameter to the request's content):
public class PassengersContact
{
public Passenger[] Passengers { get; set; }
public Contactinfo Contactinfo { get; set; }
}
and now for your controller (this is just a test controller):
[RoutePrefix("api")]
public class DefaultController : ApiController
{
[HttpPost]
// I prefer using attribute routing
[Route("addBusOrder")]
// FromUri means that the parameter comes from the uri of the request
// FromBody means that the parameter comes from body of the request
public IHttpActionResult addBusOrder([FromUri]string userUniqueId,
[FromUri]int platFormId,
[FromUri]string deviceId, [FromUri]int routeScheduleId,
[FromUri]string journeyDate, [FromUri]int fromCityid,
[FromUri]int toCityid, [FromUri]int tyPickUpId,
[FromBody]PassengersContact passengersContact)
{
// Just for testing: I'm returning what was passed as a parameter
return Ok(new
{
UserUniqueID = userUniqueId,
PlatFormID = platFormId,
RouteScheduleId = routeScheduleId,
JourneyDate = journeyDate,
FromCityid = fromCityid,
ToCityid = toCityid,
TyPickUpID = tyPickUpId,
PassengersContact = passengersContact
});
}
}
Your request should look something like this:
POST http://<your server's URL>/api/addBusOrder?userUniqueId=a&platFormId=10&deviceId=b&routeScheduleId=11&journeyDate=c&fromCityid=12&toCityid=13&tyPickUpId=14
Content-Type: application/json
Content-Length: 110
{
"passengers" : [{
"passengerName" : "name",
"age" : 52
/* other fields go here */
}
],
"contactinfo" : {
"name" : "contact info name",
/* other fields go here */
}
}
Notice the api/addBusOrder comes from concatenating the values of the RoutePrefix/Route attributes.

parse nested json string in c#

i have json string as
{"AccountNo":"345234533466","AuthValue":"{\"TopUpMobileNumber\":\"345234533466\",\"VoucherAmount\":\"100\"}"}
to parse this string i have created class as
public class UserContext
{
public string AccountNo { get; set; }
public string AuthValue { get; set; }
}
in AuthValue it gives me output as {\"TopUpMobileNumber\":\"345234533466\",\"VoucherAmount\":\"100\"} which is absolutely correct. now i want to modify my class in such way that i want AuthValue in string format as well and in seprate member variable format.
so i modify my class in this way but it gives error
public class UserContext
{
public string AccountNo { get; set; }
public string AuthValue { get; set; }
public Auth ????? { get; set; }
}
public class Auth
{
public string TopUpMobileNumber { get; set; }
public string VoucherAmount { get; set; }
}
My requirement is
AuthValue whole json string i required
in another variable i want member wise values
Parsing Logic
UserContext conObj1 = new UserContext();
conObj1 = JsonConvert.DeserializeObject<UserContext>(context);
Note : No modification in json string is allowed.
I'm not very familiar with JsonConvert or Json.NET so I'm not sure what options are available for that. Personally I'd just call the deserializer again immediately afterwards.
UserContext conObj1 = new UserContext();
conObj1 = JsonConvert.DeserializeObject<UserContext>(context);
conObj1.AuthObject = JsonConvert.DeserializeObject<Auth>(conObj1.AuthValue);
You could move this into the class if you wanted and call it directly off the deserialized class.
public class UserContext
{
public string AccountNo { get; set; }
public string AuthValue { get; set; }
public Auth AuthObject { get; private set; }
internal UserContext Deserialize()
{
// Serialize the object
this.AuthObject = JsonConvert.DeserializeObject<Auth>(this.AuthValue);
// Return this object for a simple single-line call.
return this;
}
}
// Single object
UserContext conObj1 = new UserContext();
conObj1 = JsonConvert.DeserializeObject<UserContext>(context).Deserialize();
// Enumeration of object (given that this is supported in JsonConvert)
IEnumerable<UserContext> conObjs = JsonConvert.DeserializeObject<IEnumerable<UserContext>(contexts).Select(c => c.Deserialize()).ToList();
Or if you feel self hating you could go as far as doing the deserialization at the time the property is accessed (although I would avoid this at almost all costs due to the numerous issues it can cause).
public class UserContext
{
private Auth m_auth;
public string AccountNo { get; set; }
public string AuthValue { get; set; }
public Auth AuthObject
{
get
{
if (this.m_auth == null)
{
this.m_auth = JsonConvert.DeserializeObject<Auth>(this.AuthValue);
}
return this.m_auth;
}
}
}
I would suggest using two classes - one for the JSON you're actually receiving, and then one for the object model you want to use:
public class JsonUserContext
{
public string AccountNo { get; set; }
public string AuthValue { get; set; }
}
public class UserContext
{
public string AccountNo { get; set; }
public Auth AuthValue { get; set; }
}
public class Auth
{
public string TopUpMobileNumber { get; set; }
public string VoucherAmount { get; set; }
}
...
var jsonUserContext = JsonConvert.DeserializeObject<JsonUserContext>(json);
var authJson = jsonUserContext.AuthValue;
var userContext = new UserContext {
AccountNo = jsonUserContext.AccountNo,
AuthValue = JsonConvert.DeserializeObject<JsonUserContext>(authJson);
};

Categories