I have a domain name: TestSite.com. I create several subdomains for this site and refer to them as first.TestSite.com, second.TestSite.com, etc.
How do I refer to TestSite.com relatively without having to hard code its name in an html or aspx file in first.TestSite.com? What I mean is (using folders as example) if I have a folder TestSite and a sub folder first
TestSite/first
, then from first I can refer to its parent TestSite folder by
../
What do I use to refer to TestSite.com from first.TestSite.com? Thanks.
There's no way using pure relative links. You have to program it as a string manipulation.
Something like:
var host = location.host;
var lastPeriod = host.lastIndexOf(".");
var remainder = host.substring(0, lastPeriod);
var afterSecondLastPeriod = remainder.lastIndexOf('.') + 1
var baseDomain = host.substring(afterSecondLastPeriod);
console.log(baseDomain);
EDIT: Shorter version using regex:
var baseDomain = host.match(/[^.]*\.[^.]*$/)[0]
This is general, so it will always return the last part. Regardless of whether it's a.TestSite.com, b.a.TestSite.com, etc. it will return TestSite.com.
You will have to modify it if this assumption is not correct.
If you want to keep your relative links, you can use the base element.
[The base element's href] attribute specifies an absolute URI that acts as the base
URI for resolving relative URIs.
So, add a base element, specify the href you'd like, and all the relative URIs on the page now use the URI you've specified as the base URI.
<head>
<base href="http://testsite.com" />
</head>
That's all you need. However, if you'd like to make things a little cleaner, you can pull that URI from the web.config using System.Configuration.ConfigurationManager.AppSettings. Here's an aspx snippet:
<head>
<base href="<%= ConfigurationManager.AppSettings["rootdomain"] %>" />
</head>
And the web.config:
<configuration>
<appSettings>
<add name="rootdomain" value="http://testsite.com" />
</appSettings>
</configuration>
This method allows you to affect many elements from one and its value can be driven from the web.config.
Since you tagged your question ASP.NET, I'm going to assume you want to do this server-side, so you are not relying on JavaScript on the client-side to generate HTML links.
First of all, make sure you have a web.config file in your subdomain sites, and add this to the configuration:
<configuration>
<appSettings>
<add name="MainDomain" value="example.com" />
</appSettings>
</configuration>
Using this setting in your ASPX pages, hyperlinks on one of your subdomains' pages would look like this:
Go to main domain home page
Go to products page on the main domain
The setting with name="MainDomain" will be read from the configuration file, and inserted into the page where it says <%$ AppSettings: MainDomain %>. This way, you are creating the required HTML on the server-side, with no hard-coding required.
If you need to change the name, simply update the configuration file, and all pages will automatically use the new domain name.
An alternate solution would be to use the original domain name for the live server, but point it to your development environment.
You can override DNS lookups in Windows by adding lines to this file.
C:\Windows\System32\drivers\etc\hosts
Add the following using a text editor.
127.0.0.1 www.livesite.com
127.0.0.1 first.livesite.com
127.0.0.1 second.livesite.com
These domains will now map to the locahost. So there won't be a need to hack HTML for testing.
Related
Is there any way to compute rendered property based on some JavaScript condition?
For Example:
if document.compactMode == "BackCompat"
All examples I find; use SSJS #{javascript:condition;}
For example:
<resource rendered="#{javascript:context.getUserAgent().isIE(0,6)}">
<content-type>text/css</content-type>
<href>global.css</href>
</resource>
How can it be mixed something like this:
<![CDATA[return document.compactMode == "BackCompat"? true: false;]]>
You cannot use CSJS to compute something server side. This is conceptual not possible, because the CSJS is executed in the browser and themes are calculated on the server.
What you can do: Execute some CSJS and send the result to the server. On the server you then can process the this data and perform your computation for the theme properties.
F.e. you can open an landing page, execute your CSJS and then redirect to the target XPage with some URL parameters or a cookie or... Then you can access the data on server side.
<resource rendered='#{javascript:context.getUrlParameter("CSJS") == "1"}'>
<content-type>text/css</content-type>
<href>global.css</href>
</resource>
If you open the XPage with URL ?CSJS=1, the global.css will be added.
Perhaps it would make more sense to modify your CSS so that a single CSS file handles multiple browsers? For example, see http://webdesignerwall.com/tutorials/css-specific-for-internet-explorer
Right now, I'm having trouble.
First of all I have a page, let's call it "http://blah.com/login".
That obviously goes strait to "index.asp"
A line of Main.asp:
<!--#include file="resource/menu.asp"-->
Page top includes all of what I need for my menu... so:
Part of resource/menu.htm:
<div id="colortab" class="ddcolortabs">
<ul>
<li><span>Main</span></li>
...
</ul>
</div>
<!--Main drop down menu -->
<div id="dropmain" class="dropmenudiv_a">
Announcements
Contact Information
Meeting Plan
Photo Gallery
Upcoming Events
</div>
Let's say I click on the "announcements" (http://blah.com/login/main/announcements.asp) link... Now I'm at the announcements page!
But wait, I include the same menu file. Guess what happens:
I get sent to "http://blah.com/login/main/main/announcements.asp
Which doesn't exist...
My solution:
Make a menu_sub.asp include for any subpages.
But wait a second... this WORKS, but it gets REALLY REALLY messy... What can I do to use just one main "menu.asp" instead of "menu_sub.asp"? using "/main/announcements.asp" WON'T be an option because this is a web application that will be on different directories per server.
There is no magic bullet fix. Here is how I would do this.
Create a separate include file that DIM's and Initializes global variables called info.asp
Inside info.asp DIM a variable called strRelativePath and initialize it to ""
Include info.asp at the top of each asp page above the menu.asp include
Modify menu.asp and include <%=strRelativePath%> in all the urls
Inside each asp page set strRelativePath as necessary:
<!--#include file="includes/info.asp"-->
strRelativePath = "Login/"
<!--#include file="resource/menu.asp"-->
Add this function to your menu.asp:
Function GetApplicationPath()
GetApplicationPath = Mid(Request.ServerVariables("APPL_MD_PATH"), Len(Request.ServerVariables("INSTANCE_META_PATH")) + 6) & "/"
End Function
Dim prefix: prefix = GetApplicationPath()
Now structure your references in the menu.asp using:
Announcements
This will make these references absolute but they will vary appropriately with whatever virtual folder path the website has your ASP application installed under.
There is a similar question here where the highest rated answer states that you can find the root with the following 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:
ToRootedVirtual("/")
or
ToRootedVirtual("/index.asp")
etc. to return the root from the server.
You can use Virtual instead of File because Virtual accepts an absolute path from the root directory, not relative like File.
Like this:
<!--#INCLUDE VIRTUAL="/resource/menu.asp"-->
Also, make sure your IIS settings allow Includes, See this link for details:
http://tech.mikeal.com/blog1.php/server-side-includes-for-html-in-iis7
This can be fixed with the HTML base tag:
Add this tag in the beginning of each page:
<base href="http://blah.com/login">
EDIT: I solved by myself.Cause the stylesheet reference path is /css/style.css not css/style.css,url will not be fixed by asp.net.I found that <link> <meta> and <title> will add as an server control to head when head tagged with runat="server",So these server control will auto fix current reference problem.
!!!BUT,<script> is ignored,One of the solutions is <%= ResolveClientUrl('~/js/jquery.js') %>.But It does not work when you have a theme attched to master page,asp.net canot add stylesheets in App_Themes to head when it contains such <% %> expression,asp.net will throw an exception.
So it seems like the best solution is using <ScriptManager>.
Another important discover is that when you have a <ContentPlaceHolder> in master runat=server head,stylesheet inside <Content> of child page using this master page will not be treat as a server control.So in <Content> of child page you must use <%= ResolveClientUrl%> to handle url fix.
ORINGIAL:
I'm running asp.net 4.0 on IIS 7 Express. I route "MarketList/{type}" to "~/MarketList.aspx" with default value new {type = 0}. The URL "localhost:4888/MarketList" just works well, I can recieve default value "0".
But I found that the "/" will mess up the stylesheet and javascript references defined in "Main.master" master page. "Main.master" is in the root level with "MarketList.aspx". The stylesheet in "Main.master" is defined as css/style.css. The "css" folder is also at the root level. When I'm accessing by "/MarketList", it works well. But, "/MarketList/1" gives the value "1" to {type}. The URL of the stylesheet in the page becomes "../css/style.css" which points to "/MarketList/css/style.css". This doesn't exists (obviously).
So, I decide to use "-" to split those parts, I route "market-list-{type}" with same setting just like above. But, I found I can not access the default routing URL which I thought would be "localhost:4888/market-list-". "localhost:4888/market-list" does not work either. Only "/market-list-0" will work.
Could someone help me?
HI All,
I need your suggestions/idea on this.When we started the project,client said that we want
to accept Potentially Dangerous HTML Tags(like img,script,link etc...) into textbox,and we save textbox value into database.
Now,Client want to allow these tags ,but want to save these values in encoded form(to avoid xss)
Now, I just wanted to know that is there any way in asp.net ,so that I can replace behavior of all the existing textboxes with new SafeTextbox with minimal changes(I don't want to add HtmlEncode or HtmlDecode to each textbox.)
For example
public class NewTextBox:TextBox
{
public override string Text
{
get { return HttpUtility.HtmlDecode(base.Text); }
set { base.Text = HttpUtility.HtmlEncode(value); }
}
}
At runtime
ProjectTextBox replaces with NewTextBox
You could look into TagMapping in ASP.NET.
http://msdn.microsoft.com/en-us/library/ms164641(v=vs.100).aspx
The tagMapping element defines a collection of tag types that are remapped to other tag types at compile time. This remapping causes the mapped type to be used in place of the original tag type for all pages and controls in the ASP.NET application within the scope of the configuration file.
<pages>
<tagMapping>
<add tagType="System.Web.UI.WebControls.TextBox"
mappedTagType="Yournamespace.NewTextBox"
/>
</tagMapping>
You can set ValidateRequests to true at page level or for all pages from web.config.
Not sure if this would help, but have you considered control adapters?
http://www.singingeels.com/Articles/How_To_Control_Adapters.aspx
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
CSS in App_Theme folder gets Cached in Browser
I've seen "What is an elegant way to force browsers to reload cached CSS/JS files?" but the answer there uses PHP and it doesn't address the fact that the CSS is injected dynamically by an ASP.Net Theme.
I think I have a quick and dirty solution. The trick is to examine the controls within the page header (for example in the PreRender phase), find the links pointing to CSS-files under the App_Themes folder and make them dynamic (by adding some random information to the query-string). This will most likely tell the browser to invalidate the cached version of the file.
The code:
protected void Page_PreRender(object sender, EventArgs e)
{
HtmlLink link = null;
foreach (Control c in Header.Controls)
{
if (c is HtmlLink)
{
link = c as HtmlLink;
if (link.Href.IndexOf("App_Themes/", StringComparison.InvariantCultureIgnoreCase) >= 0 &&
link.Href.EndsWith(".css", StringComparison.InvariantCultureIgnoreCase))
{
link.Href += string.Format("?t={0}", DateTime.Now.Ticks.ToString());
}
}
}
}
The output:
<link href="App_Themes/MyTheme/MyTheme.css?t=634310637798128189"
type="text/css" rel="stylesheet" />
Note that you need to have a <head runat="server"> declared in your page's markup in order to be able to access the Header property (otherwise it will be null).
If you are asking how the SERVER side can force reloading... One way is to dynamically change the filename of the CSS/JS so that subsequent calls to the page require a different file.
<sarcasm>
The other is to simply tell the user to press CTRL-F5 :)
</sarcasm>
When finished doing changes to the site change the name of the css manually.
I've not found a way to version App_Themes files (yet), but you can set them to expire in a relatively short period of time (e.g., 10 seconds), which will minimize the cache problem while giving you some performance benefit of caching. Remember, if the file has not changed, the server will respond with 304-Not modified so traffic is reduced even if the browser requests the file.
Add this to the <configuration> section of the Web.Config
<location path="App_Themes">
<system.webServer>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="00:00:10" />
</staticContent>
</system.webServer>
</location>