1

I'm having a very weird case in my querystring for [FromUri]. The code below is my model parameter

public class PagingParams
{
    public PagingParams()
    {
        // set the default values
        this.PageNo = 1;
        this.PageSize = 30;
    }

    public int PageNo { get; set; }
    public int PageSize { get; set; }
    public string OrderBy { get; set; }
}

This is my controller code.

[Route("search")]
[ResponseType(typeof(PagingList<EmailTemplatesInfo>))]
public async Task<IHttpActionResult> Search(SearchParams searchOption, [FromUri] PagingParams p)
{
    // Check
    if (searchOption == null) return BadRequest("Invalid search options");

    // Filter EmailTemplate by Keyword
    var emailTemplate = db.EmailTemplates.Where(et => et.Name.Contains(searchOption.Keyword) ||
                                                      et.Description.Contains(searchOption.Keyword)).ProjectTo<EmailTemplatesInfo>();

    // Filter by Status
    emailTemplate = emailTemplate.Where(et => searchOption.Status.Contains(et.Status));

    // Check & Set
    if (p == null) p = new PagingParams();

    // Set Default Sort
    if (string.IsNullOrEmpty(p.OrderBy)) p.OrderBy = DEFAULT_ORDERBY;

    return Ok(new PagingList<EmailTemplatesInfo>(p, emailTemplate));

}

Base on the code above if i want to pass in Parameter Binding for PagingParam.

Should be this url

  • search?PageNo=1&PageSize=10&OrderBy=CreatedOn

But the result i get in swashbuckle it become

  • search?p.PageNo=1&p.PageSize=10&p.OrderBy=CreatedOn

the object name for PagingParam need to append in the querystring

2
  • Can you try to debug your application using the expected query string and see if the parameters were bound correctly inside the controller action? I'm thinking if they were bound correctly, then the issue might be with swashbuckle. Commented Mar 28, 2016 at 0:46
  • @OJRaqueño I tried it require me to append the p. to ensure the parameter passed in to the model.. Commented Mar 28, 2016 at 0:47

2 Answers 2

4

The correct way to fix it: [FromUri(Name = "")] PagingParams p

Sign up to request clarification or add additional context in comments.

Comments

0

I found a fix which.
Required to write IFilterOperation to overwrite it..
It might not the best way but it solve my problem.

public class HandleFromUriParam : IOperationFilter
{
    public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
    {
        string[] splitter = null;
        var fromUriParams = apiDescription.ActionDescriptor.GetParameters().
            Where(param => param.GetCustomAttributes<FromUriAttribute>().Any()).ToArray();

        foreach (var param in fromUriParams)
        {
            var fromUriAttribute = param.GetCustomAttributes<FromUriAttribute>().FirstOrDefault();

            // Check
            if (fromUriAttribute != null)
            {
                var operationParam = operation.parameters;

                foreach (var item in operationParam)
                {
                    if (item.name.Contains(param.ParameterName))
                    {
                        splitter = item.name.Split('.');
                        item.name = splitter[splitter.Length - 1];
                    }
                }
            }
        }
    }
}

Comments

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.