0

In my DataTables, I want to avoid repetition and keep the base address in web.config file. Then, I just need to concatenate the variable part. Here's what I've done without success and the message from browser (IE 11):

"DataTables warning: table id=configurationTable - Ajax error..."

Javascript:

$(document).ready(function () {
        var url = @Html.Raw(Json.Encode(ConfigurationManager.AppSettings["BaseAddress"])) + "Configuration";
        $('#configurationTable').DataTable({
            ajax: {
                'url': "'" + url + "'", //url is concatenated correctly
                'dataSrc': 'value',
            },...edited for brevity

Config setting:

<add key="BaseAddress" value="http://myServer:8083/api/"/>

Update: I also have tried this without putting url in quotes. Chrome show a 406 error - Not Acceptable

Update 2: The Web API service I'm calling has OdataControllers and ApiControllers. The prefixes for both are the same, so the OData ones need to be configured first in the WebApiConfig. Plus, no quotes on the url argument.

3
  • What error are you getting? (follow this to get the error code/description: datatables.net/manual/tech-notes/7) Commented Oct 20, 2015 at 19:25
  • Try to change it to 'url': url,. There is no need to add single quotes. Commented Oct 20, 2015 at 23:54
  • @AlexArt...In Chrome it shows a 406 - Not Acceptable Commented Oct 21, 2015 at 11:22

2 Answers 2

1

You should not wrap URL in single quotes.

Also I'm not ASP.NET expert, but according to this answer your code should be changed to:

$(document).ready(function () {
    var url = '<%= ConfigurationManager.AppSettings["BaseAddress"] %>' + 'Configuration';
    $('#configurationTable').DataTable({
        ajax: {
           'url': url,
           'dataSrc': 'value'
        },
        // ... skipped ...
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

I tried this too, and no luck - same error. It's a 406 - Not Acceptable
@BigDaddy, there could be some other issue unrelated to URL that is causing it. As long as you see request made to the correct URL, it means that solution with <%= ConfigurationManager.AppSettings["BaseAddress"] %> works.
You're right. My routing was bad on the OData service I'm calling
0

As you're using MVC, I'm assuming your js files are separate, ie not included in the View. If this is the case then accessing the value using Razor syntax won't work.

Try this:

In the model that you send to the View, add a property for the web.config setting:

public string BaseAddress
{
    get { return ConfigurationManager.AppSettings["BaseAddress"]; }
}

This way, when you pass the model to the view, it always has the BaseAddress value. in the View you add a hidden field:

@Html.HiddenFor(x => x.BaseAddress, new { id = "baseaddress" })

And your datatables initialisation js code can retrieve it:

url: $("#baseaddress").val() + '/Configuration';

1 Comment

Thanks, please see my Update 2 comment.

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.