locked
Pass key in web.config to add in herf dynamically RRS feed

  • Question

  • User-34860367 posted

    Hi all,

    I got several Razor page cshtml which having href for popup modals. I got an issue to change herf path manually when deploying them to the server.

    I would like to change the href path dynamically.

    For example: I want to add server published path /MyApp/Test before to all the following href as follows:

    1. In index.cshtml: <a class="btn btn-sm btn-success" data-modal="" href="/Student/View" id="btnView"><span class="glyphicon glyphicon-list"></span>&nbsp;View Student</a>

    2. In Create.cshtml

    <a class="btn btn-sm btn-success" data-modal="" href="/Student/Create" id="btnCreate"><span class="glyphicon glyphicon-plus"></span>&nbsp;Add Student</a>

    3. In Edit.cshtml

    <a class="btn btn-sm btn-success" data-modal="" href="/Student/Edit" id="btnEdit"><span class="glyphicon glyphicon-edit"></span>&nbsp;Edit Student</a>

    In web.config 

     <appSettings>
        <add key="serverPath" value="/MyApp/Test" />
      </appSettings>

    I am not sure how to create key in web.config and pass it to them dynamically. Is it possible to implement that automated process?

    Thanks in advance.

    Wednesday, October 25, 2017 3:02 PM

Answers

  • User-34860367 posted

    I used ASP.NET MVC 5. I figured out what went wrong with relative server path as follows:

    Just remove / in each href="/Student/Create", etc. in my code, then It will map to server path correctly. 

    Thanks anyway.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 25, 2017 4:40 PM

All replies

  • User753101303 posted

    Hi,

    Or try :

    href="~/Student/View"

    The ~/ symbol is processed server side and is the root of the web application so it will be /Student/View or /MyApp/Test/Student/View depending on where you published your web app.

    Edit: not sure in which MVC version it was introduced. If using still an older version you may have to explicitely use @Url.Content("~/Student/View/") instead...

    Edit2: try perhaps https://docs.microsoft.com/en-us/aspnet/web-forms/overview/older-versions-getting-started/master-pages/urls-in-master-pages-cs It is for Web Forms it is basically the same principle in MVC as well.

    Wednesday, October 25, 2017 3:35 PM
  • User-34860367 posted

    Hi,

    I tried it and it was working at all in my popup modal. If I add it manually in each HREF, the modals will popup. 

    One of the reason why your solution was working because I hoover on action icon, there is ~ symbol before URL.

    Any other idea? Thanks.

    Wednesday, October 25, 2017 3:57 PM
  • User753101303 posted

    Ok so first which MVC/Razor version do you use ? I tried :

    <a href="~/somewhere">Test 1</a>
    <a href="@Url.Content("~/somewhere")">Test 2</a>

    which gives :

    <a href="/somewhere">Test 1</a>
    <a href="/somewhere">Test 2</a>

    or if published to a test app root folder :

    <a href="/test/somewhere">Test 1</a>
    <a href="/test/somewhere">Test 2</a>

    If option 1 doesn't work because you are using an old MVC/Razor version you'll have to use option 2 which explicitely calls Url.Content.

    Edit: I'm not sure to fully understand but ~ should never be seen client side. It is processed server side to create an url (basically ~/ is replaced with the root of your web app that is / or /test/ or whatever...)

    Wednesday, October 25, 2017 4:12 PM
  • User-34860367 posted

    I used ASP.NET MVC 5. I figured out what went wrong with relative server path as follows:

    Just remove / in each href="/Student/Create", etc. in my code, then It will map to server path correctly. 

    Thanks anyway.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 25, 2017 4:40 PM
  • User1771544211 posted

    Hi avt2k7,

    Thanks for your share.

    Best Regards,

    Jean

    Thursday, October 26, 2017 1:55 AM
  • User753101303 posted

    It works from the parent folder of the Student folder but may not work from other places. It's really weird that ~ doesn't work. It should be fine with MVC 5.

    Thursday, October 26, 2017 9:13 AM