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;
               
            }
6
  • 1
    I'd write my own small helper and use WebUtility to encode filter value. S. a. stackoverflow.com/questions/44920875/… Commented May 15, 2020 at 12:39
  • is this MVC? if so could you show the controller? Commented May 15, 2020 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 Commented May 15, 2020 at 12:43
  • Check out this answer Commented May 15, 2020 at 12:45
  • 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. Commented May 16, 2020 at 18:35

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

Comments

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

            }

1 Comment

Thank you .. i have added controller method

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.