This is a migrated thread and some comments may be shown as answers.

[Solved] Generate custom url

4 Answers 191 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Mario
Top achievements
Rank 2
Mario asked on 03 Dec 2010, 03:42 PM
Hi,

We're using a grid with paging and custom data binding. We also provide a route name to the databinding select.
This works fine, except one thing, the url's appends a querystring of the route data values which we don't need (or want) to show.

Here's an example:

<% Html.Telerik().Grid(Model)
       .Name("ProductsGrid")
       .PrefixUrlParameters(false)
       .EnableCustomBinding(true)
       .DataBinding(d => d.Server().Select("CmsRoute")
       .Pageable(s => s.Total(Model.TotalItems).PageSize(Model.PageSize))
       .Sortable()
       .Columns(columns =>
...
  ).Render(); %>

As you can see, I'm using a route name to select from the server - "CmsRoute".
This is a custom route that we use to make our url's look something like this:

/Something/Something_else/123456.html
OR
/Whatever/how/many/times/you/want/123456.html

The last part is extracted with regex in our custom MvcRouteHandler. And to make everything work, we add some stuff to the RequestContext.RouteData.Values;

protected override System.Web.IHttpHandler GetHttpHandler(RequestContext requestContext)
{
    // extract id and get other stuff
    ....
 
    requestContext.RouteData.Values.Add("cmsid", cmsid);
    requestContext.RouteData.Values.Add("linkedobjectid", linkedobjectid);
    requestContext.RouteData.Values.Add("actionid", actionid);
    requestContext.RouteData.Values.Add("viewname", view.ViewName);
    requestContext.RouteData.Values.Add("controller", controller.ControllerNameShort);
    requestContext.RouteData.Values.Add("action", action.ActionName);
 
    return base.GetHttpHandler(requestContext);
}

Later on we use these values to find what we need.

Now, as I said before, the Grid is appending these route data values as a querystring;

public class GridUrlBuilder
{
  / ..... /
    public RouteValueDictionary PrepareRouteValues(RouteValueDictionary routeValues)
    {
        RouteValueDictionary result = new RouteValueDictionary(routeValues);
 
        result.Merge(grid.ViewContext.RouteData.Values, false);// Here's where the "magic" happens.
         
        if (grid.EnableCustomBinding)
        {
            result[grid.Prefix(GridUrlParameters.PageSize)] = grid.PageSize;
        }
 
        foreach (string key in grid.ViewContext.HttpContext.Request.QueryString)
        {
            if (key != null && !result.ContainsKey(key))
            {
                result[key] = grid.ViewContext.HttpContext.Request.QueryString[key];
            }
        }
 
        return result;
    }
}

Maybe this is by design, but how about an option or delegate where we can build the url?
Or an option to disable the merging?

Or is there another way of doing this, I mean, remove the querystring values we don't need?

Regards,
Mario

4 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 03 Dec 2010, 03:51 PM
Hello Mario,

 Indeed this is by design. The grid copies all query string values in order to preserve any state the developer may be transferring when paging, sorting etc is done. This also comes in handy when there is more than one grid in the page - paging the first grid must not lose the state of the second grid.

 Unfortunately there is no way to disable this with a setting. The only option is to modify the source code. You can check the GridUrlBuilder.cs file if you want to do that.

Kind regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Mario
Top achievements
Rank 2
answered on 03 Dec 2010, 04:14 PM
Thanks for the fast reply!

That's too bad.

Wouldn't it be possible to add an if-statement for result.Merge(grid.ViewContext.RouteData.Values, false); ?

Something like:

Grid/Binding builder:
<% Html.Telerik().Grid(Model)
       .Name("ProductsGrid")
       .PrefixUrlParameters(false)
       .EnableCustomBinding(true)
       .DataBinding(d => d.Server().Select("CmsRoute").MergeRouteData(false))
       ....
       .Render(); %>

OR

<% Html.Telerik().Grid(Model)
       .Name("ProductsGrid")
       .PrefixUrlParameters(false)
       .EnableCustomBinding(true)
       .DataBinding(d => d.Server().Select("CmsRoute"))
          .MergeRouteData(false)
       ....
       .Render(); %>


GridUrlBuilder:
public class GridUrlBuilder
{ 
    public RouteValueDictionary PrepareRouteValues(RouteValueDictionary routeValues)
    {
        RouteValueDictionary result = new RouteValueDictionary(routeValues);
           
        if (grid.MergeRouteData) // Or something?
              result.Merge(grid.ViewContext.RouteData.Values, false);
         
        if (grid.EnableCustomBinding)
        {
            result[grid.Prefix(GridUrlParameters.PageSize)] = grid.PageSize;
        }
 
        foreach (string key in grid.ViewContext.HttpContext.Request.QueryString)
        {
            if (key != null && !result.ContainsKey(key))
            {
                result[key] = grid.ViewContext.HttpContext.Request.QueryString[key];
            }
        }
 
        return result;
    }
}

Sure, I could do this myself, but that would mean more work on future telerik mvc releases.
And you probably have a better idea for this =)

Is this possible? Can I make a feature request?

Regards,
Mario
0
Atanas Korchev
Telerik team
answered on 03 Dec 2010, 04:20 PM
Hi Mario,

I will log that as a feature request but I am not sure if we will be able to implement that any time soon. 


Regards,

Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Mario
Top achievements
Rank 2
answered on 03 Dec 2010, 04:25 PM
Well, that's always something, anyway =)

Thank you, Atanas!
Tags
Grid
Asked by
Mario
Top achievements
Rank 2
Answers by
Atanas Korchev
Telerik team
Mario
Top achievements
Rank 2
Share this question
or