1

I am developing angular 2 application using visual studio 2015, In my current project I want to get the app setting value from web.config in typescript file(service.ts)

Before posting this question into this, I did google it and read some links but I did not get it. This is the link I read earlier but I did not get a result: https://forums.asp.net/t/2071477.aspx?How+to+read+web+config+appsetting+key+value+in+Type+Script

Please tell me how to solve this problem.

1

2 Answers 2

0

As any xml file you can do something like :

        let url : string = path_to_your_file;
        let observableResult: Observable<any>
            = this.http.get(url)
            .map(res => res.text())
            .subscribe((xml : any) => {
              let parser = new DOMParser();
              let webConfig= parser.parseFromString(xml, 'text/xml');
              let nodes = webConfig.getElementsByTagName('add');

              for (let i = 0; i < nodes.length; i++) {
                     parse_property(nodes.item(i));
              }, ...)
         }
Sign up to request clarification or add additional context in comments.

3 Comments

I am new to Type Script. So, Please mention the complete code for how to parse the web.config file using typescript.
Web.config is just an xml file. This code is nearly ready to use. The difficulty is to expose your web.config via http. Maybe you will prefer a solution like Remy's. And the "parse_property" function depends on your own application... You may just replace it by a console.log to see a result...
I copied above code and past it in my typescript file where ever I want. but I gave the path to my web.config file like this "./web.config" . is this correct path to my web.config file or not. and also observableResult variable shows error like "Type subscription is not assigned to type observable<any>".
0

You cannot access a web.config file directly from TypeScript. You have to write it into your html document first with some type of ServerSide code. You can store the setting either in a hidden html text field or inside a javascript variable. Then you can read that text field or javascript variable in Typescript.

This has been answered before here: Reading web.config from JavaScript

1 Comment

I don't know how to write the code, please tell me or ping example code here.

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.