Relative links with Razor in ASP.NET without MVC - c#

We're working on a site that was set up with all the pages in the root directory. As a result all the links are referencing locations in the same directory they're located in by default...
Page
We've restructured it and some of the pages are now in sub folders, so the links throughout the site now need to be relative. I've read that a tilde (~) won't work in the markup when using Razor. ie Page
How can a link be formatted so that it points to something in the root directory?
Something like? Page
EDIT: Resolved with: Page

Try this:
#helper _href(string url)
{
#VirtualPathUtility.ToAbsolute(url)
}
To use it on a page:
linky
Edit: How I never knew about the built-in Href method is beyond me. You should definitely use that instead.

Use the Href method, like this:
#Href("SomePage")
Note that you don't need to use file extensions.

Glance at #Html.ActionLink
It allows you use controller name

Related

Getting Current Page URL From Master Page with Routing

I'm trying to be able to determine which page url the user is loading from the master page.
So far I've been able to use
this.Request.RawUrl
to get the path of the page itself, which works fine for most cases.
However in this particular website, we use a lot of complicated routing, so something like (say)
/Product/5/2/Purchase
might redirect to /Purchase.aspx?ID=5Type=2
of which I'd want the actual aspx file path.
I've also tried this.Request.PhysicalPath, but that doesn't give the route and basically just appends the path the user requested to the virtual directory.
So how can I do it?
An asp.net Page treats a master page as just another control.
So if you want to get the page, you can always use the Page property provided by the MasterPage class.
I used this and worked :
Page.ToString().Replace("ASP.","").Replace("_",".")

Simple Html Menu in asp.net.. Broken url issue

When I use simple menu built manually in html and css presents problem with the navigation. When I navigate a page that is inside a folder when i navigate away to somewhere outside of the current folder then the previous folder remains in the url and page not found message appears. I am currently in development stage. not deployed.
sounds like you need to use absolute URLs, not relative ones. so:
http://www.blah.com/category/page.html
or just
/category/page.html
NOT
page.html
It's because you don't return to your root when you try to redirect
e.g.
I have a page called Default.aspx that isn't in a folder and a page Page.aspx in the folder called SubFolder.
Your urls probably look like this to redirect:
Default.aspx
SubFolder/Page.aspx
When you try to redirect from Page.aspx to Default.aspx, you don't return to the root, so the code will look for a page called Default.aspx in the folder SubFolder, but it doesn't exist, that's why you're getting a page not found error.
You should write your urls like this:
~/Default.aspx
~/SubFolder/Page.aspx

Generic Urls for set of asp.net internal pages

I am looking for a solution that will allow me to print generic url for a set of pages.
Example:
For pages - site.com/About/Contact.aspx, site.com/About/WhoWeAre.aspx, site.com/About/Members.aspx etc., user should see only site.com/About/ in the address bar.
Is that something achievable? This site is not SEO friendly and requires users to login before accessing content, also I don't expect site.com/About/ or any internal pages to resolve to any page when typed directly on browser. I am also fine if real url is printed on status bar if the user hovers on the internal links. I don't think URL Rewrite or URL Routing works here. Or may be I am missing something. Using .NET 3.5 and C#.
I am tagging sitecore because I am more interested in sitecore based solution where I have different nodes under About tree and I want users to see only upto /About in the address bar, but I think if it can be done in asp.net, I can figure out sitecore part.
As you are interested in sitecore, I can tell you how we have done custom URL in a recent sitecore build.
You need to have a custom link manager by extending the current LinkProvider.LinkBuilder sitecore class. Then you need to add your custom logic on how you would like the URL by overriding GetItemUrl and BuidlItemUrl to display URL and finally and it in the web.config for sitecore to use your custom link manager.
Absolutely, use the asp.net 4 routing engine.
This will give you exactly what you want:
http://weblogs.asp.net/dotnetstories/archive/2011/01/03/routing-in-asp-net-4-0-web-forms.aspx
EDIT: for 3.5 check out:
Friendly URLs for ASP.NET
The only way to solve this, keeping /About in the address bar, would be to create client-side code that switches out the content based on user selection.
You can do that by loading the About page and then load the content for any child pages using a postback or using ajax calls.
You could also put an iframe on the /About page, and have the navigation links point the iframe to the other pages.
I think you might be able to use the target attribute to target the iframe, or use javascript to change the src of the iframe.

Referencing links in a different way

I am new to C# and asp.net and would like to know the following:
Can I reference jquery library in the following format?
src="http://~/sites/booksite/tools/js/jquery.tools.min.js"
it does not give a compilation error or anything but want to make sure..
Thank you
The ~/ (tilde+slash) method of referencing paths is an ASP.NET thing - URLs on elements with runat="server" (i.e. server-side controls) will be evaluated and expanded from the relative path (where ~/ is the root of the application or virtual directory.) If the ASP.NET engine isn't doing this, then it doesn't get done.
In order to specify a relative path from the root, you should be able to get away with just the slash:
src="/sites/booksite/tools/js/jquery.tools.min.js"
Alternatively, apply the runat="server" value, and it would work:
runat="server" src="~/sites/booksite/tools/js/jquery.tools.min.js"
But when using the tilde+slash, then http:// won't work.
No. if the jquery is local to your site you can use the ~ to represent the root of your site. Provided that this src attribute is on a control with runat=server. But providing the http:// is unneeded in that case.
No you must definetly cannot. The "root folder" of your "application" is managed by the server, your application doesn't care if it's in / (development machine) or /prettyapp (production server).
What you can do however is ask ASP.NET figure out the path for you and fill it in:
<script src='<%= ResolveUrl("~/sites/booksite/tools/js/jquery.tools.min.js") %>'></script>
As a node, don't just use absolute paths (/something/) like Mr. Disappointment suggests, your application will die if you deploy it to a virtual directory.
This isn't the right way.
The Tilde will only be processed if the tag has runat="server", so yes you can do it but you shouldn't.
The ONLY reason you would want to specify a full http path to the script file is if you are hosting it via a content delivery network (CDN).
However, jQuery is already hosted on arguably the largest CDN -> Google. See http://code.google.com/apis/libraries/devguide.html
So, I would just leverage the resources that they give for free.

Best practices, How to specify relative path for css,javascript and images files?

I have a question about relative path to external javascript, css and images files in Asp.C# application.
I have pages with 2 - 5 sub levels so my javascript and css files looks like
../../../../../javascriptfile.js
../../../cssfile.css
../../../../../../image.jpg
In case if it will be additional sub level application won't find files.
What is the best practices to specify path to file???
Keep in mind when considering these answers that "root-relative" and "root of the site" may really mean the root of the path following the domain name in the url. You may need to take into account scenarios where your web site is not located at the root. In such scenarios, root-relative paths would potentially point to a different web site.
In ASP.NET you can use a leading ~ to generate urls relative to the root of current site for most server-side controls, as in:
<img src="~/image.jpg" runat="server">
You can also use the ResolveUrl method (and other similar methods) to expand such paths without using server-side controls.
Use a root-relative path like this:
/js/javascriptfile.js
/css/style.css
the first / means at the root of the site.
Use absolute or root-relative paths to avoid confusion in multi-level pages.
Look here for more information:
http://www.motive.co.nz/glossary/linking.php

Categories