question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Binding DataSourceLoadOptions in ASP.NET Boilerplate dynamic controllers

See original GitHub issue

Started in https://github.com/DevExpress/DevExtreme.AspNet.Data/issues/231#issuecomment-396456741


If I understand correctly, you are talking about ASP.NET Boilerplate dynamic controllers. According to their docs, model binders should work (see our docs and example). Replacing fields with properties won’t make binding automatic.

When model binders don’t work, you can use DataSourceLoadOptionsParser directly from an action method:

var loadOptions = new MyLoadOptions(); // a subclass of DataSourceLoadOptions
var query = Request.GetQueryNameValuePairs();
DataSourceLoadOptionsParser.Parse(loadOptions, name => query.FirstOrDefault(i => i.Key == name).Value);

or

var loadOptions = new MyLoadOptions(); // a subclass of DataSourceLoadOptions
DataSourceLoadOptionsParser.Parse(loadOptions, name => Request.QueryString[name]);

@AlekseyMartynov correct. If I make a subclass with getters/setters and then return back a DataSourceLoadOption it works… My original issue is the dynamic controller created an empty parameter list for the the DataSourceLoadOption class.

Not sure if youre familiar with the framework but the dynamic api converts app services over to webapi and js actions.

EX: You see how the 2nd method has no input variables. (If I make a subclass with getters/setters etc, it generates the parameters)

[DontWrapResult]
public virtual object GetAllDevExtremeByJsonOptions(string input, bool withIncludes = true)
{
   CheckGetPermission();
   
   DataSourceLoadOptions options;
   if (!string.IsNullOrEmpty(input))
   {
       options = JsonConvert.DeserializeObject<DataSourceLoadOptions>(input, new JsonSerializerSettings {ContractResolver = new DefaultContractResolver()});
   }
   else
   {
       throw new UserFriendlyException("LoadOptions is Null!", "The load options should not be empty for a DevExtreme end point!");
   }

   return GetAllDevExtreme(options, withIncludes);
}

[DontWrapResult]
public virtual object GetAllDevExtreme(DataSourceLoadOptions input, bool withIncludes = true)
{
   CheckGetPermission();

   return DataSourceLoader.Load(GetAllQueryable(withIncludes), input).ToJsonString(false, true);
}
// action 'getAllDevExtremeByJsonOptions'
abp.services.app.bRC.getAllDevExtremeByJsonOptions = function(input, withIncludes, ajaxParams) {
  return abp.ajax($.extend(true, {
    url: abp.appPath + 'api/services/app/BRC/GetAllDevExtremeByJsonOptions' + abp.utils.buildQueryString([{ name: 'input', value: input }, { name: 'withIncludes', value: withIncludes }]) + '',
    type: 'GET'
  }, ajaxParams));;
};

// action 'getAllDevExtreme'
abp.services.app.bRC.getAllDevExtreme = function(input, withIncludes, ajaxParams) {
  return abp.ajax($.extend(true, {
    url: abp.appPath + 'api/services/app/BRC/GetAllDevExtreme' + abp.utils.buildQueryString([{ name: 'withIncludes', value: withIncludes }]) + '',
    type: 'GET'
  }, ajaxParams));;
};

This is what I was trying to do: (which now works if I just pass the JSON over and convert in the method above, Is there a better way that you know of?)

var gridData = new DevExpress.data.DataSource({
    load: function(loadOptions) {
        abp.log.debug("Initial Load Options:");
        abp.log.debug(loadOptions);
        abp.log.debug(stringToPascalJSON(loadOptions));
        var deferred = $.Deferred();
        abp.services.app.bRC.getAllDevExtremeByJsonOptions(JSON.stringify(stringToPascalJSON(loadOptions)))
            .done(function(result) {
                abp.log.debug("Response");
                abp.log.debug(result);
                deferred.resolve({ data: result.data, totalCount: result.totalCount });
            });
        return deferred.promise();
    },
    byKey: function(key) {
        abp.log.debug("ByKey: " + key);
        return { id: key };
    }
});

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
AlekseyMartynovcommented, Jun 13, 2018

It’s great to hear that you have found a good solution. Another good case for using RemoteController!

To make it even better, I can suggest to use Url.Content to deal with relative URLs:

.RemoteController().LoadUrl(Url.Content("~/api/services/..."))
0reactions
bbakermmccommented, Jun 15, 2018

This can be closed, I figured out my issues finally. Part of them were related to the fact that the webapi is dynamic and that its webapi and only allows for 1 parameter. So I made a method that took a jobject and parsed the data out of that rather than passing in 1 object to body and rest via querystring.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Building Dynamic Web API Controllers
ASP.NET Boilerplate creates Api Controllers on runtime. ASP.NET Web API's model and parameter binding is used to bind models and parameters. You can...
Read more >
Articles Tutorials | AspNet Boilerplate
AspNet Boilerplate (ABP) is an open source and well-documented application framework. See the comprehensive tutorials of ABP for technical information.
Read more >
Building Dynamic Web API Controllers
'/api/services/' is a prefix for all dynamic web api controllers. ... NET Web API's model and parameter binding is used to bind model...
Read more >
module system documentation
AspNet Boilerplate (ABP) is an open source and well-documented application framework. See the comprehensive tutorials of ABP for technical information.
Read more >
Registering XML Localization Sources
The ILocalizationSource interface should be implemented by a localization source. It is then registered to ASP.NET Boilerplate's localization configuration.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found