How to find page in folder when using master page asp.net - asp.net

When my pages are in the main level of the solution, the menu item href can find the content pages. When I put the content pages in a subfolder and change the href path respectively it cannot find the page. This seems to only be a problem with MasterPages. Am I missing something here? I get the error
Server Error in '/' Application The resource cannot be found HTTP 404.
Requested URL: /Pages/Pages/Items.aspx
Why does it add another /Pages in front of my path?
Solution
Project
+Images
-Pages
Items.aspx
Library.aspx
+Styles
Default.aspx
Site.Master
MasterPage
<div id="tabdiv" class="tabdiv">
<ul id="tabmenu" class="tabmenu">
<li>Items</li>
<li>Library</li>
</ul>
</div>
<div id="main" class="main">
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>

Short answer, use an absolute path (to the root of your site) or use ~/ to relate all pages back to the site root (latter being more secure and robust between server location changes).
Reasoning?
When you're within the root (/) linking to Pages/Items.aspx combines these two: /Pages/Items.aspx.
When you're already in Pages/ you're now re-citing the same directory Pages/Items.aspx so you're getting a combined value of /Pages/Pages/Items.aspx.
When you want to link to items, have a look at the Control.ResolveUrl method. e.g.
Items
Now, no matter what page that link is embedded it, it still will provide an absolute path to that resource (in this case, Items.aspx).

try like this
<li>Items</li>
<li>Library</li>

Use thge above
Items

Remove the Pages from the href. The master page is a wrapper for the .aspx pages' content, but you should use the relative path to the .aspx page when you provide the links inside the master page (given that all your pages are inside the Pages folder):
<li>Items</li>
<li>Library</li>
Another option would be to prefix with /, which will render the link by using the root-relative link/root link/web-root relative link:
<li>Items</li>
<li>Library</li>

If your tag is "a href" you must put runat = "server" with their respective id = "lnkItem" then the code of the page from the server side you should write:
lnkItem.HRef = ResolveUrl("~/Pages/Items.aspx");
If you are calling to another site you can use 'ResolveUrl':
Response.Redirect(ResolveUrl("~/Pages/Items.aspx"));

Give a path like this.Include your Domain name.
<li>Items</li>
Or
You should use XML file or website path file.

Related

Razor Page - How to redirect to another folder's page using asp-for tag helper

I've been having issues with this but I think is simple.
asp.net core 2.0 Razor Pages
I have a Users/index page and I added a link to take me to the child records under Entries Folder.
Structure is like
Pages
/Users
/Entries
Under /Pages/Users/index.cshtml
<a asp-page="./Edit" asp-route-id="#item.Id">Edit</a> |
<a asp-page="./Details" asp-route-id="#item.Id">Details</a>|
<a asp-page="~/Pages/Entries/" asp-route-id="#item.Id">Enter Child Records</a>
However, on the browser, looks like is not rendering the correct link. It staying under the default page.
I tried asp-page="../Entries/" and other combinations with no luck.
Can't believe this but this works.
<a asp-page="../Entries/Index" asp-route-id="#item.Id">test</a>
I think your own answer might add confusions to others. First of all, you're talking about LINKs to another page, not redirect to another page. Razor Pages have a designated function for page redirections.
About the asp-page tag helper, it's interpreted by the code-behind engine (e.g. C# syntax) and so the "~" doesn't work. Basically, asp-page is expecting a name. In your own examples:
<a asp-page="./Edit" asp-route-id="#item.Id">Edit</a> |
<a asp-page="./Details" asp-route-id="#item.Id">Details</a>|
will work, but they should be shortened as:
<a asp-page="Edit" ...>Edit</a> |
<a asp-page="Details" ...>Details</a>|
because they are in the same folder as your /Users/Index page. As you know the "./" part just means going up one level to your parent folder and so it's redundant. You don't want to instruct your engine to go up one level and then come down again to the same folder immediately.
When you need to switch to another containing folder, going up two levels and specify the new folder name. So, the "../NewFolder" works.
If it is at the web site root level, you can use the root syntax directly for faster routing. So both of these work the same if AnotherFolder is under root:
<a asp-page="../AnotherFolder/Index" ...>test</a>
<a asp-page="/AnotherFolder/Index" ...>test</a>
The asp-page tag helper is expecting a real page name at the last part and hence the quoted text must be ended with a page name. You cannot use asp-page="/Entries/" as in HTML URLs and expect the engine to default to Index page for you.
Lastly, to cover redirecting pages, code-behind has a function called return RedirectToPage(string pageName, ...), and its first parameter uses the same page routing syntax as the asp-page tag helper described above.
There is also a Redirect() function that works with pages outside your Razor Pages system, which can take any URLs as your browser or HTML code do. That's another story.

Relative paths of images contained in an ajax aspx page

In my webapp, I have a folder Views. In this folder several .aspx pages live. I don't use the rendered contents directly in the webapp, rather I request the contents using ajax in a main aspx page on the root of the webapp.
Now when I refer to an image, "images/image.png" will work since the image reference lives in the aspx page on the root. When I change this to "/images/image.png", this won't work since the root is determined by the virtual IIS folder.
How can I have a clean reference e.g. "/images/image.png"?
Firstly and probably most elegant, make the element run at the server and use the root-reletive url:
<img src="~/images/image.png" alt="image" runat="server" />
This will automatically translate your src-value into a path which will resolve from your current location. However, there is one caveat. If you do it on pages which are included, asp.net may create an incorrect value here, as it could be morphed into
<img src="../images/image.png" alt="image" />
if you are a directory down. So if you include this result in a page in the application root folder, your value may not be correct. I have not seen a good way to work around this. It will, however, show you a warning if the file doesn't exist.
Alternatively, you may want to manually set the root path for such pages:
<img src="<%=Request.ApplicationPath %>/images/image.png" alt="image" />
which will transform into a path always coming from the root of the site:
<img src="/AppPath/images/image.png" alt="image" />
Obviously, this is a bit more verbose. Additionally, you will not be able to see any warnings if the referenced file does not exist, as it will be dynamically built.

<img> tag in masterpage does not show logo when called from IIS virtual folder?

It works fine when I dont use virtual folder. My virtual folder is named test which points to an application inside MyDocuments. The path to my App is
localhost\test\app\login.aspx
Note that if I move the application in the root folder wwwroot and make it an application, it works fine. I tried
<img src="logo.jpg" />
<img src="..\logo.jpg" />
<img src="~/logo.jpg" />
<img src="\\test\logo.jpg" />
Can it be fixed or should I leave it? My logo.img is in root folder of the application. I move it to \images\ folder as well still does not work.
There is a similar post here Relative Path in master page for img tag which did not solve my problem because it does not use Virtual Folder path.
Edit: I did used tag also and it did not work too.
<asp:Image ID="imgLogo" runat="server" ImageUrl="~/logo.jpg" />
Thanks in advance
Try adding runat="server" within the your html img contrl and select the src="" from the intellisense property of the visual studio.
or
Use Asp image server control instead of html img control and set the imageurl attribute from intellisense property of the visual studio.
Hope this will help you...
Use the asp:Image, it does all the hard work for you and gets rid of this kind of problem.
The ~/logo.jpg syntax only works in server controls, such as <asp:Image />. The ~ is then a shorthand for the root of your web-application.
If your 'test' directory is an application, then the logo should be there to be found.
To troubleshoot these kind of problems, you need to know the mapping between the physical location of your page ('login.aspx') and the url used to call it. A similar mapping will exist between the physical location of your image and the url you need to get it.
You could try to enter the url for that image directly in the browser. When you have a url that succeeds, you can figure out how to refer to that image from your page.
If it's in the same directory, a plain 'logo.jpg' will work. If elsewhere, you need to add some folderpaths ('images/logo.jpg' if it's in a folder named 'images' next to that page).
I had the same problem and above solutions worked for me.
I know this is old post.
In Masterpage all you have to do is
drag and drop Image
then go its properties and set the url (You can browse it from there)
Now all my pages have the logo with no issue of finding it.

Why does my localhost image url change

I have a bunch of images in my localhost folder (C:\inetpub\wwwroot\Images) which I am trying to access within my ASP.net application. The image src generated in my markup is:
<img id="MainContent_MainImage" src="localhost/Images/FGOStuart_7166.jpg" />`
This fails to load the image and if I look at the source for the page it actually directs to
http://localhost:64395/Pages/localhost/Images/FGOStuart_7166.jpg
so it looks like it is trying to access a path relative to the page (on the Pages folder). The src works if I type it into the browser manually and the image is displayed.
Can anyone explain what's going on here and how to fix it? I'm attempting to move the images out of the database and onto the file system but without much luck so far.
That's because the browser assumes "localhost" is a folder and adds it to the current relative path. Add http to it and it should work fine, or remove localhost altogether and just leave the /Images... part.
Try it like this:
<img id="MainContent_MainImage" src="~/Images/FGOStuart_7166.jpg"
alt="An Image" runat="server" />
This resolves it server-side from the root down. And always use an alt :)
What you really want to be doing is using the magic tilde:
<img id="MainContent_MainImage" runat="server" src="~/Images/FGOStuart_7166.jpg" />
~ signifies the root of the application. Notice I added runat="server", too.

Base URL in ASP.net Master Pages with virtual Directories

I have an ASP.net master page. In this master, I have all my css and javascript files defined. I also have a few images and a few buttons and hyperlinks.
All the urls are all declared as relative ie "/scripts/ian.js"
Everything works fine if this site is the root website, but I need it to work in a virtual directory.
My problem is when I place this website in a virtual directory under a root site, all my links are pointing to the root site. so my links point to www.root.com/scripts/ian.js but it should be pointing to www.root.com/virtualDir/scripts/ian.js
I thought the Base Href tag in the header would help, but so far it does not seem to be helping in anyway. All the links are still pointing to the root website when i hover over them.
What I would like is a single setting either in IIS or the config file that I can set a root url and any image, script or link either on the master page or content page, would point to the right place.
Any suggestions or ideas are welcome.
Thanks
All the urls are all declared as
relative ie "/scripts/ian.js"
Those seem to be absolute URL's that you're using, rather than relative URL's, which is probably why the <base /> tag isn't having the desired effect:
This attribute specifies an absolute
URI that acts as the base URI for
resolving relative URIs.
-- from http://www.w3.org/TR/html401/struct/links.html#h-12.4
You could try removing the leading '/' from your URL's to see if that works?
Failing that, I tend to use ResolveClientUrl to get around issues like this, which you'd use in the same way as others have suggested using ResolveUrl:
<script type="text/javascript" src="<%= ResolveClientUrl("~/path/to/js") %>"></script>
...
<img src="<%= ResolveClientUrl("~/path/to/img") %>" alt="..." />
Hope this helps.
Most tags, including regular HTML tags like <link>, <img>, etc can use the ~/ as the application root path if the *'runat="server"' attribute is set.
eg.
<img src="~/images/test.png" runat="server" />
This makes tag a server tag and the tilde is replaced with the application root before the output is returned to the browser.
This doesn't work as expected for the <script> though. When 'runat="server' is set for the script tag, then the script is considered to be server-side javascript and execution is attempted.
To work around this you can either inject the javascript using one of the register client script methods. your you can use the <%= ResolveUrl('~')%> tag in your script tag.
This static method returns you full http path to root folder of your application (web site or virtual directory)
public static string GetAppRootUrl(bool endSlash)
{
string host = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority);
string appRootUrl = HttpContext.Current.Request.ApplicationPath;
if (!appRootUrl.EndsWith("/")) //a virtual
{
appRootUrl += "/";
}
if (!endSlash)
{
appRootUrl = appRootUrl.Substring(0, appRootUrl.Length - 1);
}
return host + appRootUrl;
}
So, you can write in master page:
<script src="<%= Server.HtmlEncode(GetAppRootUrl(false)) %>/scripts/ian.js" language="javascript" type="text/javascript"></script>
Use tilde(~) in yout reference (i.e ~/scrips/ian.js)...see if it works
For links try Page.ResolveUrl in the .aspx page.
So I found this IIS weirdness last night:
<script src="/js/file.js"></script>
Will not work properly in a virtual application that's in a subdirectory of the main IIS site.
Instead, you MUST do it like this:
<script src="/js/file.js" type="text/javascript"></script>
Which is the standard way to do it, but if you're not looking for it, it can surprise you that that additional tag makes the relative path issues go away.

Resources