3

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;
               
            }
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Ajt
  • 1,719
  • 1
  • 20
  • 42
  • 1
    I'd write my own small helper and use `WebUtility` to encode filter value. S. a. https://stackoverflow.com/questions/44920875/url-encode-and-decode-in-asp-net-core – Christoph Lütjen May 15 '20 at 12:39
  • is this MVC? if so could you show the controller? – Patrick Mcvay May 15 '20 at 12:39
  • I have controller in MVC that accepts complex query string in that I am calling api ...so I want to append query string object to call api controller – Ajt May 15 '20 at 12:43
  • Check out this [answer](https://stackoverflow.com/questions/54531257/controller-method-doesnt-catch-object-routes-from-redirect/54531531#54531531) – Alexander May 15 '20 at 12:45
  • @ChristophLütjen No need to reinvent the wheel: [Flurl](https://flurl.dev/docs/fluent-url/) has URL building built in. – mason May 15 '20 at 17:43
  • 1
    @mason - cool lib. I don't like that it adds extension methods to string class and I'd argue that in this case a custom method would be much easier to use and less error prone. – Christoph Lütjen May 16 '20 at 18:35

2 Answers2

4

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);
Ajt
  • 1,719
  • 1
  • 20
  • 42
2

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}";

            }
MemeDeveloper
  • 6,457
  • 2
  • 42
  • 58