none
Need to add the value from web config to the HTML page RRS feed

  • Question

  • Hi

    I have one settings in webconfig like below.

    <appSettings>
    <add key="MyAlertsPageUrl" value="https://google.co.in"/>
    </appSettings>

    I need to access that value in my HTML page when i loads.

    <p class="font">
          Click <a href="????" data-targettype="webpage">here</a>
    </p>

    Just wanted to understand how to reference in place of ???? to get the value of "MyAlertsPageUrl" which is there in the 

    webconfig.

    How to read it from value from webconfig to HTML page.

    Regards,

    Sunil


    Tuesday, August 20, 2019 5:57 AM

Answers

  • Hi Sunil,

    According to your description, I don’t suggest you directly read the web.config by using javascript in html page. This is not security.

    I suggest you could try to use the ajax to send the request to the MVC code-behind and get it back to the client side.

    More details, you could refer to below codes:

    MVC Home controller:

        Function GetAppSetting() As String
            Return ConfigurationManager.AppSettings("MyAlertsPageUrl")
        End Function
    

    Html:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <title></title>
        <script src="Scripts/jquery-3.3.1.min.js"></script>
        <script>
            $(function () {
       $.get( "home/GetAppSetting", function( data ) {
      $( "#GetApp" ).attr( "href",data  );
      alert( "Load was performed." );
    });
            })
        </script>
    </head>
    <body>
        <p class="font">
            Click <a href="" id="GetApp" data-targettype="webpage">here</a>
        </p>
    </body>
    </html>
    

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sunil A M Thursday, September 12, 2019 12:13 PM
    Wednesday, August 21, 2019 1:37 AM
    Moderator

All replies

  • Hi Sunil,

    Thank you for posting here.

    Based on your description, you want to read the value from webconfig in html page.

    You could use the following code.

    <p class="font">
    Click <a href="<%= ConfigurationManager.AppSettings["MyAlertsPageUrl"] %>" data-targettype="webpage">here</a>
    </p>

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 20, 2019 7:02 AM
    Moderator
  • Hi,

    Thanks for the response.

    I have tried the way u told but still its not working,

    Please find the attached screen for reference.

    

    Regards,

    Sunil

    Tuesday, August 20, 2019 7:14 AM
  • Hi

    Thanks for the feedback.

    You could download it to see if it works.

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, August 20, 2019 7:28 AM
    Moderator
  • Hi,

    My project is mvc and built using vb.net.

    Here i am opening one raw HTML page when clicking on some result. in that raw HTML i have above issue.

    Its working for you because you did the same thing in the webform1 asp.net server page.

    Could you please help me how to implement above in the raw static HTML page.

    Regards,

    Sunil

    Tuesday, August 20, 2019 7:46 AM
  • Hi Suni,

    You also could try the following code.

     <p class="font">
            Click <a href=@System.Configuration.ConfigurationManager.AppSettings("MyAlertsPageUrl"), data-targettype="webpage">here</a>
        </p>

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, August 20, 2019 8:48 AM
    Moderator
  • Hi,

    I have tried the above code. Now the issue is the link is opening as 

    localhost:222222/@System.Configuration.ConfigurationManager.AppSettings("MyAlertsPageUrl"),

    Its not opening the actual link which is mentioned in the MyAlertsPageUrl

    Regards,

    Sunil

    Tuesday, August 20, 2019 9:43 AM
  • Hi Sunil,

    According to your description, I don’t suggest you directly read the web.config by using javascript in html page. This is not security.

    I suggest you could try to use the ajax to send the request to the MVC code-behind and get it back to the client side.

    More details, you could refer to below codes:

    MVC Home controller:

        Function GetAppSetting() As String
            Return ConfigurationManager.AppSettings("MyAlertsPageUrl")
        End Function
    

    Html:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <title></title>
        <script src="Scripts/jquery-3.3.1.min.js"></script>
        <script>
            $(function () {
       $.get( "home/GetAppSetting", function( data ) {
      $( "#GetApp" ).attr( "href",data  );
      alert( "Load was performed." );
    });
            })
        </script>
    </head>
    <body>
        <p class="font">
            Click <a href="" id="GetApp" data-targettype="webpage">here</a>
        </p>
    </body>
    </html>
    

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sunil A M Thursday, September 12, 2019 12:13 PM
    Wednesday, August 21, 2019 1:37 AM
    Moderator
  • If your page is MVC, you should read the MyAlertsPageUrl into model that you pass back to view.

    Say, it's okay to have static method to read data from database and put the result on certain position of a page directly, but normal people writing MVC will still put the data on a model first, then pass the model instead.

    Wednesday, August 21, 2019 1:48 AM
    Answerer