Last post Oct 26, 2017 09:13 AM by PatriceSc
Oct 25, 2017 03:02 PM|avt2k7|LINK
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> 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> 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> Edit Student</a>
<add key="serverPath" value="/MyApp/Test" />
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.
Oct 25, 2017 03:35 PM|PatriceSc|LINK
Or try :
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.
Oct 25, 2017 03:57 PM|avt2k7|LINK
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.
Oct 25, 2017 04:12 PM|PatriceSc|LINK
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...)
Oct 25, 2017 04:40 PM|avt2k7|LINK
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.
Oct 26, 2017 01:55 AM|Jean Sun|LINK
Thanks for your share.
Oct 26, 2017 09:13 AM|PatriceSc|LINK
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.