Relative path for a included file - ASP/HTML - asp-classic

Sorry if this question is answered somewhere else but I tried searching several pages and was unsuccessful.
So i have an include file (sidebar) which i am using in all pages.
Default.asp
Products.asp
Salary/Survey.asp
inc/sidebar.asp (this is the included file)
now inside sidebar.asp I have a link for Salary/Survey.asp
from all other pages at root level, i can simply use href='Salary/Survey.asp' and will work fine. but when I am on page Survey.asp , writing href='Salary/Survey.asp' will become actually Salary/Salary/Survey.asp. I understand it has to be ../Salary/Survey.asp to be used properly but it will then not work for root level pages.
I can not use root relative which is /Default.asp and /Salary/Survey.asp as I am working for someone else' project and i dont know his directory structure and thus i only have option to document relative path.
Hope this is clear to understand and someone helps me out.
Thanks!

We solved this problem the following way...
Each of our asp pages included a special file that Dims and sets golbal variables. We called ours Info.asp
Inside Info.asp we defined a variable called strRelativePath
Dim strRelativePath
strRelativePath = ""
Every asp page set the relative path according to it relative position:
for example:
Root pages - strRelativePath = ""
One level deep pages - strRelativePath = "../"
Two levels deep pages - strRelativePath = "../../"
Then it was a matter of prefacing all the links requiring a relative path with <%=strRelativePath%>

you need to get write this after the that - Salary/Survey.asp
You can get the virtual path to the file from one of several server variables - try either:
Request.ServerVariables("PATH_INFO")
Request.ServerVariables("SCRIPT_NAME")
Either server variable will give you the virtual path including any sub-directories and the file name - given your example, you'll get /virtual_directory/subdirectory/file.asp. If you just want the virtual directory, you'll need to strip off everything after the second forward slash using whatever method you prefer for plucking a directory out of a path, such as:
s = Request.ServerVariables("SCRIPT_NAME")
i = InStr(2, s, "/")
If i > 0 Then
s = Left(s, i - 1)
End If
or:
s = "/" & Split(Request.ServerVariables("SCRIPT_NAME"), "/")(1)

basically, if your sidebar can be included from programs in different folders, the only 'easy' way is to use absolute paths like you mentioned.
You say can't use it, so I would think of different ways...
virtual folders: In IIS you could set a virtual folder in salary folder for 'salary' and point it to the site's root.
OS links (similar to above, but at the OS level)
use mappath. You could check mappath to see the actual folder you're in, and use the correct include (with/without /salary) though I'm thinking this might give you an error, not sure.

Related

ASPX FriendlyUrls MapPageRoute generalised for all folder paths

We are in the process of rebuilding an old web ASPX site to implement FriendlyUrls (yes, a little slow off the mark :-)).
Within Global.asax we can implement:
Routes.MapPageRoute("ItemAction", "FolderPath/{ItemKey}/{ItemAction}", "~/FolderPath/Default.aspx");
This gives ItemKey & ItemAction for a specific FolderPath - all good.
However, we have about 80 FolderPath to satisfy & more may be added later.
That would mean 80 hard coded lines of MapPageRoute().
We are looking for a way to generalise this into as few hard coded statements as possible.
We had thought of loading the Route Map into a static (perhaps XML or DB) file to load, parse & execute the MapPageRoute() in a loop. However, this still requires making sure the edit of folder paths is correct, and the web site would require a restart (its on a shared commercial host).
What we are looking for is something like either:
Routes.MapPageRoute("ItemAction", "{FolderPath}/{ItemKey}/{ItemAction}", "~/{FolderPath}/Default.aspx");
Routes.MapPageRoute("ItemAction", "*/{ItemKey}/{ItemAction}", "~/*/Default.aspx");
That way we can add new folder paths without updating anything.
As background, we have been using Web.config to achieve something like this - the current URLs look like "/FolderPath/ItemAction_ItemKey.aspx" e.g. "/FolderPath/Save_MyItem.aspx". We are looking to drop the ".aspx" and turn to more conventional, current URL formats.

Can [parameters] be inserted dynamically into paths in Next.js?

I'm running into a problem trying to get routes working with [params]/:params.
The paths I'm working with will always be structured as follows:
console/sites/[siteId]/possibly-another-directory/page.
The console/sites portion I can set as basePath in next.config.js but the [siteId] is something I would rather not have to manually add to every path in order to not end up with console/sites/possibly-another-directory/page. Is there a single place I can set this so that it will be included in every path I link to from within my site?
FWIW, I've tried setting my basePath to /console/sites/:siteId but :siteId was being read as a literal value.

How can I change this .NET code to File.Exists?

I am working with a framework written in .NET and I do not know .NET. I just need to change this one line where it checks to see if a variable exists, and I need to change it to instead just check on the server to see if the file itself exists.
Here is what is there now:
#if (!string.IsNullOrEmpty(Model.DrawingLink2){
Is this the correct code to change it to check if the file exists instead?
#if (File.Exists(/Portfolio/#(Model.FileNumber)/Images/Large_#(Model.FileNumber)_1.jpg))
You need to map that file, relative to the root of the web application, to the physical file system. You can use HttpServerUtility.MapPath for that. You also need quotes around string literals. The process running the code also needs read access to the directory (very likely the case, just mentioning it to be complete).
#if (File.Exists(HttpServerUtility.MapPath("/Portfolio/#(Model.FileNumber)/Images/Large_#(Model.FileNumber)_1.jpg"))

Rewrite urls from user submitted HTML

I use one WYSIWYG editor in a small cms. It allows users to upload files, images, etc. If I add image named dog.jpg, in source I'll get:
<img src="/myweb/userfiles/images/dog.jpg" />
I can save this to a database and use it later on any page, until I move my site to a live domain.
myweb is virtual directory in IIS. "/" points to root, in this case localhost, so I have to use "/myweb". However when I upload site to server and copy database there, all links will be broken, because there is no "myweb" folder on server.
My idea was to replace "/myweb" on save with empty string. I also have to replace full url, which editor creates for some files. On display I would have to add correct Application dir. I would probably save both versions in database, and only on server change force display version to update.
By now I've come up with:
p = p.Replace("href=\"" + fullUrl, "href=\"").Replace("src=\"" + fullUrl, "src=\"").Replace("href=\"" + partialUrl, "href=\"").Replace("src=\"" + partialUrl, "src=\"");
Which is ugly, hard to maintain and inefficient. I guess better approach would be to use regex, but I don't know how to do it.
My question is, can anyone recommend good article, blog/forum post on this? If you have other solution, great.
I am unsure the regex version has any of the characteristics you mention in this case.
That said, you can do:
string ReplaceUrlPaths(string html, string partialPath, string fullPath)
{
var pattern = string.Format("((href|src)=\")({0}|{1})", partialPath, fullPath);
var regex = new Regex(pattern);
return regex.Replace(html, m => m.Groups[1].Value);
}
[TestMethod]
public void TestMethod10()
{
var input = #"<img src=""/myweb/userfiles/images/dog.jpg"" />";
//carefull with any special regex char in the paths
var replaced = ReplaceUrlPaths(input, "/myweb", "/some/full/path");
Assert.AreEqual(
#"<img src=""/userfiles/images/dog.jpg"" />",
replaced);
}
If you are proceeding with that, refactor it to instantiate the regex once with the compile option(as the partialPath and fullPath won't be changing).
Also consider avoiding it all, by defining a web site with an alternate port to just have it as a root Url.
Store the local root path separately from the images. For each image, store the relative path to that image.
When displaying images locally, use the local root merged with the relative path. When you publish to the remote server, you add the remote root to the relative path.
Does your WYSIWYG editor allow you to configure the base URL e.g. so that paths to images can use relative paths? I think the FCKEditor has something like FCKConfig.BaseHref in it's config file that does something like this.
Alternatively can you run the site as a root site using the ASP.NET 2.0 web server? Then you wouldn't have to worry about rewriting paths to images as you could just use paths from the webroot.

Get virtual path for a full path in asp classic

How can I get the virtual path for a full path in ASP classic. Note that the full path may be under a virtual directory and therefore the simplistic
virtPath = Replace(fullPath, Server.MapPath("/"), "")
method will not work.
Edit: To clarify, an example follows
Full Windows File Path (known):
\\MyServer\MyShare\Web\Site\Logs\Test.txt
My Website has a Virtual directory
called Logs that Points to \\MyServer\MyShare\Web\Site\Logs\.
Virtual Path (unknown): /Logs/Text.txt
Http path (unknown, needed):
http://Site/Logs/Test.txt
The code is in a asp page in the main app, not under any virtual directories. It is located on a separate server from the file in question.
IIS 6.0
How do I find the virtual path from the full file path?
In case anyone's interested, Anthony Jones' answer showed me the way to getting the application's relative root consistently. So if you have a site at http://example.com and a local development equivalent at http://localhost/example, you can find your root with this function:
Function ToRootedVirtual(relativePath)
Dim applicationMetaPath : applicationMetaPath = Request.ServerVariables("APPL_MD_PATH")
Dim instanceMetaPath : instanceMetaPath = Request.ServerVariables("INSTANCE_META_PATH")
Dim rootPath : rootPath = Mid(applicationMetaPath, Len(instanceMetaPath) + Len("/ROOT/"))
ToRootedVirtual = rootPath + relativePath
End Function
You can then call it like this to get the root path:
ToRootedVirtual("/")
Which will return:
/ on example.com
/example/ on localhost/example
You can also use it without the slash:
ToRootedVirtual("")
If I've understood the question.
Assumption
The full path is a path with in the current application or a child application. It is not a path limited to the parent nor a path into a sibling application. The desired path is relative to the current applications path.
Scenario 1
A path such as
"/someApp/someFolder/someSubFolder/file.ext"
should resolve it to:-
"~/someFolder/someSubFolder/file.ext"
(although the ~/ notation isn't something ASP classic understands).
Scenario 2
"/someApp/someSubApp/SomeSubFolder/file.ext"
you still want:-
"~/someFolder/someSubFolder/file.ext"
Scenario 3
The app is the root application of the site:-
"/someFolder/someSubFolder/file.ext"
would still become
"~/someFolder/someSubFolder.file.ext"
Solution
The key to solving this is:-
Dim sAppMetaPath : sAppMetaPath = Request.ServerVariables("APPL_MD_PATH")
For the above set of scenarios this will result in something like:-
"/LM/W3SVC/33230916/Root/someApp"
"/LM/W3SVC/33230916/Root/someApp/someSubApp"
"/LM/W3SVC/33230916/Root"
Also
Dim sInstanceMetaPath: sInstanceMetaPath = Request.ServerVariables("INSTANCE_META_PATH")
will in all the scenarios return
"/LM/W3SVC/33230916"
With some mathematical reduction we can arrive at the function:-
Function ToAppRelative(virtualPath)
Dim sAppMetaPath : sAppMetaPath = Request.ServerVariables("APPL_MD_PATH")
Dim sInstanceMetaPath: sInstanceMetaPath = Request.ServerVariables("INSTANCE_META_PATH")
ToAppRelative = "~/" & Mid(virtualPath, Len(sAppMetaPath) - Len(sInstanceMetaPath) - 3)
End Function
Although there may be a better way, I always did this by creating a config variable where I manually specify the root path that is not part of the virtual path. This is because you do not know if the site will be deployed as root, under a folder in root web, or in a virtual directory.
well, my answer isn't better than OrbMan's...
I have organized my app in such a way that every include is relative...
that is
instead of \myapp\lib\somefile.asp I use ..\lib\somefile.asp
in other cases I just do what Orbman said...
Here's how you solve root-relatie pathing in html via ASP so your site can be portable to different hosting directories.
This little snippet will produce the correct prefix to set your URLs:
Mid(Request.ServerVariables("APPL_MD_PATH"),Len(Request.ServerVariables("INSTANCE_META_PATH"))+6)
You can use this in LINKs, IMGs, hyperlinks, etc as follows:
<link href="<%= Mid(Request.ServerVariables("APPL_MD_PATH"),Len(Request.ServerVariables("INSTANCE_META_PATH"))+6) %>/assets/css/master.css" rel="stylesheet" type="text/css" />
so, code your paths to be root-relative (starts with a /) and then put this snippet right in front of that first slash, inside the quotes:
The server's virtual path is:
<%Response.Write "http://" & Request.ServerVariables("server_name") &
left(Request.ServerVariables("SCRIPT_NAME"),InStrRev(Request.ServerVariables("SCRIPT_NAME"),"/")) %>
</p>

Resources