Best way to append query string parameter to URL from object - c#

I have query string class.
public class PagingModel
{
public int PageNumber { get; set; } = 1;
public string Filter { get; set; } = "text";
}
string url = "Menu/GetMenus";
I have to generate the URI with a query string based on an object in ASP.NET Core 5 preview. Is there any built in query helper?.
Required output:
/Menu/GetMenus?PageNumber=3&Filter=text
MVC Controller:
public async Task<IActionResult> index_partial([FromQuery] PagingModel paging)
{
var data = await _apiService.GetMenusAsync(paging);
return PartialView("_IndexPartial", data);
}
Service:
public async Task<PagedList<MenuModel>> GetMenusAsync(PagingModel paging)
{
string Requiredurl = "Menu/GetMenus?page="+ paging.PageNumber;
}

I got this extension method.. No need to generate query string manually.Only class object we need to pass. i thought some one else can use the same thing ...
public static string AppendObjectToQueryString(string uri, object requestObject)
{
var type = requestObject.GetType();
var data = type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.ToDictionary
(
p => p.Name,
p => p.GetValue(requestObject)
);
foreach (var d in data)
{
if (d.Value == null)
{
continue;
}
if ((d.Value as string == null) && d.Value is IEnumerable enumerable)
{
foreach (var value in enumerable)
{
uri = QueryHelpers.AddQueryString(uri, d.Key, value.ToString());
}
}
else
{
uri = QueryHelpers.AddQueryString(uri, d.Key, d.Value.ToString());
}
}
return uri;
}
Ex: In my case i called this way.
string uri = "Menu/GetMenus";
string full_uri = QueryStringExtension.AppendObjectToQueryString(uri, paging);

With a Query String this simple I would just do
PagingModel qsData = new PagingModel();
//set qsData properties as needed
string urlWithQueryString = $"/Menu/GetMenus?{nameof(PagingModel.PageNumber)}={qsData.PageNumber}&nameof(PagingModel.Filter)}={qsData.Filter}";
However more standard is to do something like
string urlWithQueryString = this.Url.Action("GetMenus", "Menu", new PagingModel { PageNumber = 3, Filter = "text" }, this.Request.Url.Scheme);
But best solution depends on your specific case - can you add your action method definition for GetMenus ?
Update for your additional code :
Seeing as looks like you want to generate the url inside the service I would simply do this :
public async Task<PagedList<MenuModel>> GetMenusAsync(PagingModel paging)
{
string Requiredurl = $"/Menu/GetMenus?{nameof(PagingModel.PageNumber)}={paging.PageNumber}&nameof(PagingModel.Filter)}={paging.Filter}";
}

Related

Is there a way to custom order endpoints in NSwag?

I am using NSwag to generate code documentation, but would like to be able to have custom control over which order the endpoints appear on the generated swagger endpoint. Possibly via an attribute on the controller methods.
I can't see any way to achieve this. A few other posts mention alphanumeric ordering, but I need to be able to define a custom ordering. Is there any way to do this?
My SwaggerConfig
public class SwaggerApiConfig
{
public Assembly TargetWebAssembly { get; }
public Action<SwaggerUi3Settings<WebApiToSwaggerGeneratorSettings>> ConfigureUiSettings { get; }
public SwaggerApiConfig(
string uiRoute,
Assembly targetWebAssembly,
string apiName,
string description,
string team = null,
string appDocumentationUrl = null)
{
if (!uiRoute.StartsWith("/"))
uiRoute = string.Format("/{0}", (object) uiRoute);
this.TargetWebAssembly = targetWebAssembly;
this.ConfigureUiSettings = SwaggerApiConfig.BuildSwaggerUiSettings(uiRoute, this.TargetWebAssembly, apiName, description, team, appDocumentationUrl);
}
public SwaggerApiConfig(
Assembly targetWebAssembly,
Action<SwaggerUi3Settings<WebApiToSwaggerGeneratorSettings>> configureUiSettings)
{
this.TargetWebAssembly = targetWebAssembly;
this.ConfigureUiSettings = configureUiSettings;
}
public static Action<SwaggerUi3Settings<WebApiToSwaggerGeneratorSettings>> BuildSwaggerUiSettings(
string uiRoute,
Assembly targetWebAssembly,
string apiName,
string description,
string team = null,
string appDocumentationUrl = null)
{
if (!uiRoute.StartsWith("/"))
uiRoute = string.Format("/{0}", (object) uiRoute);
return (Action<SwaggerUi3Settings<WebApiToSwaggerGeneratorSettings>>) (settings =>
{
settings.SwaggerUiRoute = uiRoute;
settings.SwaggerRoute = string.Format("{0}/swagger.json", (object) uiRoute);
settings.DocExpansion = "list";
settings.GeneratorSettings.Title = apiName;
settings.GeneratorSettings.Version = string.Format("{0}", (object) targetWebAssembly.GetName().Version);
settings.GeneratorSettings.Description = description;
settings.GeneratorSettings.IgnoreObsoleteProperties = false;
settings.GeneratorSettings.GenerateKnownTypes = true;
settings.GeneratorSettings.GenerateAbstractProperties = true;
settings.GeneratorSettings.DefaultEnumHandling = EnumHandling.String;
settings.GeneratorSettings.OperationProcessors.Add((IOperationProcessor) new CallerHeaderOperationProcessor());
settings.PostProcess = (Action<SwaggerDocument>) (x =>
{
x.Security = (ICollection<SwaggerSecurityRequirement>) new List<SwaggerSecurityRequirement>();
x.Info.Contact = new SwaggerContact()
{
Name = team,
Url = appDocumentationUrl
};
});
});
}
How it then gets applied in my application plumbing
if (swaggerApiOptions != null)
{
foreach (var swaggerApiConfig in swaggerApiOptions.Configurations)
{
appBuilder.UseSwaggerUi3(swaggerApiConfig.TargetWebAssembly, swaggerApiConfig.ConfigureUiSettings);
}
}
This most likely won't give your everything you need but it's worth pointing out that SwaggerUi3Settings class contains OperationsSorter setting.

Get field from a sub object in MongoDB

I have the following document in MongoDB and I want to check if the field FileName has a specific value.
Following are my classes:
public class Invoice
{
private InvoiceMetaData _metadata = null;
private List<InvoiceColumns> _invoiceFields = null;
public InvoiceMetaData Metadata
{
get
{
if (_metadata == null) _metadata = new InvoiceMetaData();
return _metadata;
}
set { _metadata = value; }
}
public List<InvoiceColumns> InvoiceFields
{
get
{
if (_invoiceFields == null)
_invoiceFields = new List<InvoiceColumns>();
return _invoiceFields;
}
set { _invoiceFields = value; }
}
}
public class InvoiceMetaData
{
public string FileName { get; set; }
public string FileProcessedOn { get; set; }
public string DirectoryPath { get; set; }
}
I've tried using the following but it's returning false even though documents with this filename exist.
string filename = "01.png";
var collection = myDB.GetCollection<Invoice>(collection_name);
var exists = collection.AsQueryable().Any(avm => avm.Metadata.FileName == filename);
I've also tried this but it's returning nothing i.e. List count is 0.
var query = Query<Invoice>.EQ(u => u.Metadata.FileName, filename).ToBsonDocument();
var exist = collection.Find(query).ToList();
Also tried this and list Count is 0,
var filter1 = Builders<Invoice>.Filter.Eq(u => u.Metadata.FileName, filename, filename);
var result = collection.Find(filter1).ToList();
Can anyone please tell me what am I doing wrong?
Any help will be much appreciated.
I haven`t worked with this for a while. But this can be helpful for you:
how to check if a field exists in a specific document Mongodb using C#?
As i remeber, you can do something like that:
var collection = myDB.GetCollection<BsonDocument>(collection_name); // you use Invoice, try BsonDocument
var documents = collection.Find(new BsonDocument()).ToList(); // this one should get you all documents
var fieldName = "Metadata" // name of field you need data from
foreach (var document in documents)
{
// this one should contain your metadata object from document, note it is BsonDocument
var metaDataObject= document.Contains(fieldName) ? document[fieldName].ToBsonDocument() : null;
var fileName= metaDataObject!= null ? metaDataObject["FileName"] : "No file name."; // should be your file name
// you also should be able to convert to dictionary, under this namespace MongoDB.Bson.BsonDocument
// var metadataDic= metaDataObject.ToDictionary();
// var fileName= metadataDic["FileName"]; // should be your file name
}
Filter examples:
var builder = Builders<BsonDocument>.Filter; // in your example invoice, try BsonDocument
var filter = builder.Eq("FileName", fileName);
var count = collection.Count(filter);

C# custom add in a List

I have the following list of strings :
var files = new List<string> {"file0","file1","file2","file3" };
I would like to be able to add new files to this list, but if the inserted file is present in the list, I would like to insert custom value that will respect the following format $"{StringToBeInserted}"("{SomeCounter}
For instance : try to add "file0" and "file0" is already I would like to insert "file0(1)". If I try again to add "file0" ... I would like to insert with "file0(2)" and so on ... Also, I would like to provide a consistency, for instance if I delete "file0(1)" ... and try to add again "item0" ... I expect that "item0(1)" to be added. Can someone help me with a generic algorithm ?
I would use a HashSet<string> in this case:
var files = new HashSet<string> { "file0", "file1", "file2", "file3" };
string originalFile = "file0";
string file = originalFile;
int counter = 0;
while (!files.Add(file))
{
file = $"{originalFile}({++counter})";
}
If you have to use a list and the result should also be one, you can still use my set approach. Just initialize it with your list and the result list you'll get with files.ToList().
Well, you should create your own custom class for it, using the data structure you described and a simple class that includes a counter and an output method.
void Main()
{
var items = new ItemCountList();
items.AddItem("item0");
items.AddItem("item1");
items.AddItem("item2");
items.AddItem("item0");
items.ShowItems();
}
public class ItemCountList {
private List<SimpleItem> itemList;
public ItemCountList() {
itemList = new List<SimpleItem>();
}
public void DeleteItem(string value) {
var item = itemList.FirstOrDefault(b => b.Value == value);
if (item != null) {
item.Count--;
if (item.Count == 0)
itemList.Remove(item);
}
}
public void AddItem(string value) {
var item = itemList.FirstOrDefault(b => b.Value == value);
if (item != null)
item.Count++;
else
itemList.Add(new SimpleItem {
Value = value,
Count = 1
});
}
public void ShowItems() {
foreach (var a in itemList) {
Console.WriteLine(a.Value + "(" + a.Count + ")");
}
}
}
public class SimpleItem {
public int Count {get; set;}
public string Value {get; set;}
}

dotnet core Storing objects in RedIs

Using .NET Core, I am trying to save and retrieve a JSON Array of the object from Redis using IDistributedCache. Below is my code for storing and reading from Redis cache:
public void Save(string key, object content, int duration)
{
string s;
if (content is string)
{
s = (string)content;
}
else
{
s = JsonConvert.SerializeObject(content);
}
duration = duration <= 0 ? DefaultCacheDuration : duration;
Cache.Set(key, Encoding.UTF8.GetBytes(s), new DistributedCacheEntryOptions()
{
AbsoluteExpiration = DateTime.Now + TimeSpan.FromSeconds(duration)
});
}
public T Get<T>(string key) where T : class
{
var c = Cache.Get(key);
if (c == null)
{
return null;
}
var str = Encoding.UTF8.GetString(c);
if (typeof(T) == typeof(string))
{
return str as T;
}
return JsonConvert.DeserializeObject<T>(str);
}
the object that I want to store is
public class RuleLoadCollection_Result
{
[Key]
public int RuleId { get; set; }
public string RuleName { get; set; }
}
In my biz logic, am saving the object like this
public IQueryable<RuleLoadCollection_Result> GetRuleLibrary()
{
var result = _dbClient.GetRuleLibrary();
_cache.Save("TestKey", result);
return result;
}
the output here is an Array of Object.
[{"ruleId":1,"ruleName":"a1"}]
What code should I write to return the same array of objects from cache? I tried a few options, most of them gave compile or runtime errors. After the bit of browsing, I tried below, it worked, but it is giving only the first element of the array.
public RuleLoadCollection_Result GetRuleLibraryFromCache()
{
return (_cache.Get<List<RuleLoadCollection_Result>>("TestKey").First());
}
output for this is
{"ruleId":1,"ruleName":"a1"}
which I understand why, but what c# should I write to JSON array back which I saved?
below code gives the runtime error
public IQueryable<RuleLoadCollection_Result> GetRuleLibraryFromCache()
{
return (_cache.Get<IQueryable<RuleLoadCollection_Result>>("TestKey"));
}
the runtime error is:
Cannot create and populate list type System.Linq.IQueryable`1[RuleLoadCollection_Result]. Path '', line 1, position 1.
This worked.
public IQueryable<RuleLoadCollection_Result> GetRuleLibraryFromCache()
{
var result = _cache.Get<IEnumerable<RuleLoadCollection_Result>>("TestKey").AsQueryable();
return result;
}

filtering list using linq

I am writing to seek help, in how can I filter data and then merge two results, below:
Update code*
public class TestController : ApiController
{
private cdw db = new cdw();
public HttpResponseMessage get([FromUri] Query query)
{
var data = db.data_qy.AsQueryable();
if (query.startDate != null)
{
data = data.Where(c => c.UploadDate >= query.startDate);
}
if (!string.IsNullOrEmpty(query.tag))
{
var ids = query.tag.Split(',');
data = data.Where(c => c.TAG.Any(t => ids.Contains(t)));
}
if (!string.IsNullOrEmpty(query.name))
{
var ids = query.name.Split(',');
data = data.Where(c => c.NAME.Any(t => ids.Contains(t)));
}
if (!data.Any())
{
var message = string.Format("No data found");
return Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
}
return Request.CreateResponse(HttpStatusCode.OK, data);
}
}
entity class:
public partial class data_qy
{
public int ID { get; set; }
public string Name { get; set; }
public string TAG { get; set; }
public string TAG_IS { get; set; }
[Newtonsoft.Json.JsonProperty(PropertyName = "Date")]
public Nullable<System.DateTime> UploadDate { get; set; }
}
Sample Dataset:
Name Tag Tag_IS
AMCAR 2013-5 03065EAC9
ARES 2006-6RA 04009JAA9
ARES 2012-1A 04013TAB9
ATOM 2003-I A 0182690668
BACM 2006-2 AM 05950EAG3
BCAP 2007-AA3 05530VAN9
BCAP 2007-AA3 05530VAN9
BCJAF 9 C 0312888037
BLNDLN 0 0213093627
BLNDLN 0 0213093627
The underlying SQL query should resemble:
select *
from [dbo].[data_qy]
where TAG like '%78473TAC4%'
or TAG LIKE '%05946XYZ0%'
OR NAME LIKE '%TAP%'
OR NAME LIKE '%STORM%'
Using the following list method above, when I run a query such as (api/test/tag=78473,12669,05946,... (30 values)), i get a -- Exception Message:
Some part of your SQL statement is nested too deeply. Rewrite the query or break it up into smaller queries
Am I missing something. Any help would be most appreciated.
Thanks
I'm not in a situation to test this on the spot, but I suspect that either of the following should work:
public class TestController : ApiController
{
private cdw db = new cdw();
public HttpResponseMessage get([FromUri] Query query)
{
IQueryable<data_qy> data = null;
if (!string.IsNullOrEmpty(query.tag))
{
var ids = query.tag.Split(',');
var dataMatchingTags = db.data_qy.Where(c => ids.Any(id => c.TAGS.Contains(id)));
if (data == null)
data = dataMatchingTags;
else
data = data.Union(dataMatchingTags);
}
if (!string.IsNullOrEmpty(query.name))
{
var ids = query.name.Split(',');
var dataMatchingName = db.data_qy.Where(c => ids.Any(id => c.NAME.Contains(id)));
if (data == null)
data = dataMatchingName;
else
data = data.Union(dataMatchingName);
}
if (data == null) // If no tags or name is being queried, apply filters to the whole set of products
data = db.data_qy;
if (query.startDate != null)
{
data = data.Where(c => c.UploadDate >= query.startDate);
}
var materializedData = data.ToList();
if (!materializedData.Any())
{
var message = string.Format("No data found");
return Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
}
return Request.CreateResponse(HttpStatusCode.OK, materializedData);
}
}
I also suspect that you don't need to check against Null in your query, since EF will understand that when transforming the Expression Tree to SQL, but if needed, you can add it.
That would remove the need to use the foreach, the Aggregate and the call to Count. and results in a much simpler query that should use the IN operator in SQL.
Currently you're executing the same query multiple times (the call to .Any at the end will execute the query and then passing the data variable will execute it again. This can be very costly. Instead, materialize the results and act upon that, as above.

Categories