asp.net changing website root - asp.net

I am URL routing myself.
Main project only contain the URL routing code.
Different websites are placed on approot.
e.g: I have deployed VS2010 test-default-website at approot/webTest/.
I am trying to opening http://webTest/Account/Login.aspx from browser.
For that URL request, I am serving page using PageParser.GetCompiledPageInstance(), as follows:
In URL route request handler,
return PageParser.GetCompiledPageInstance("webTest/Account/Login.aspx", HttpContext.Current.Server.MapPath("webTest/Account/Login.aspx"),
HttpContext.Current);
I am getting following exception:
The file '/Account/webTest/Account/Login.aspx' does not exist.
PageParser.GetCompiledPageInstance is automatically appending '/Account' before the actual address, thus unable to find it.
How can I make it not to append '/Account' ?
Thank you.
Best Regards.

"webTest/Account/Login.aspx" is a relative path, add a slash to beginning to make it absolute: "/webTest/Account/Login.aspx"

Related

URL Rewriting in IIS7 annoyingly adds folder name to url

I'm having trouble with my URL Rewriting; IIS keeps adding the folder name to the url.
E.g. my.site.com/Login/ gets re- written to my.site.com/MY/Login
The frustrating thing is that the my.site.com/Login/works but whenever a link is clicked it adds the /MY/ to the url.
Does anyone know how to resolve this?
Existing re-write rule: (http(s)?:\/\/)?my.site.com
Rule I'm playing with (not finished) to try and detect the /MY/ and redirect to the 'proper' url
(http(s)?://)?(www.)?my.site.com/MY/
Thanks!

ASP.net MVC Change in URL of the application

the current application on MVC can be accessed through URL Like:
www.someserver.com/myapplication
Now there is a request to change it to a new URL:
www.someserver.com/NEWFOLDER/myapplication
so my question is how will the MVC behave, will I have to make any routing changes ?
Thanks !
If your links are application-relative, for instance
My Area
then you shouldn't have any problems.
If you've used site-relative links,
My Area
They'll break.
#David has the correct solution. I also keep the URL in the config file as an app setting for use in certain situations.
<%: ConfigurationManager.AppSettings["WebsiteURL"] %>/Content/Images/a.png
You shouldn't as long as your application root moves (i.e. in IIS) and your URLs all properly specified in helper methods, prefixed with "~" where appropriate.
For example, a URL in an MVC app that is specified like "~/images/foo.jpg" will resolve to "www.someserver.com/images/foo.jpg" in your current scheme. Under the new scheme, if properly re-rooted in IIS, it will resolve to "www.someserver.com/NEWFOLDER/images/foo.jpg".
If you've used absolute or strictly relative URLs, however, you may be out of luck.

Error When running asp.net Application in IIS

I have an application in asp.net.I configured it in IIS.When i running this application in IIS i getting an error;
Server "/" error:
Resource Cannot be Found
Error:404
Some of pages only produce this issues.Other forms are working perfectly.Without running application in IIS Its working perfectly.
If any one can answer plz send the answer immediatly.
Thank you
I assume that the urls for some of your pages are malformed.
Check in your app how you are building the paths.
Personally, i use to have a class like PathsUtil, where i build all of my paths for the pages,
so when i'm moving to IIS, it's extremely easy just to correct something (for instance add a virtual dir etc).
Update:
- for the PathsUtil i use Paths.resx where i have defined all of my paths like
Name Value
index /Site/index.aspx
add.user /Site/addUser.aspx
and so on.
And in PathsUtil i only take the value from the Paths.resx and i build the url:
string baseUrl = getBaseUrl() + (String)HttpContext.GetGlobalResourceObject("Paths", "index");
I've just moved to an IIS7 server, and there i've created a virtual dir "gramma"
You can note that was a piece of cake only to add "/gramma" in Paths.resx, in front of each url :)
Look for the Response.Redirect() calls in the page previous to the form you are getting the error. Just make sure that any harcoded URL for the form supplied to the Response.Redirect() call is correct.
EDIT..
Also, look for If you are navigating to a form which is in a different directory than the current one. For example:-
Response.Redirect("../SomeForm.aspx");
will redirect to an ASPX page just one level up of the current one. Each pair of dots implies one directory level up.
In thses cases, I think, you are better off using ~ (tilde) character which will always take the path from the root. So the mistakes happening because of incorrect number of dots can be minimised.Try something like this:-
Response.Redirect( this.ResolveUrl("~/Myfolder/SomeForm.aspx") );
More details for the ~ and ResolveUrl here.
This can also happen If you have window.location calls in javascript if you assign an incorrect URL to the same.

What is IIS 6.0 doing with slashes after the page name in a URL?

Had a question from a client which stumped me.
They are using IIS 6.0 and for some reason, instead of making a normal request for a page on their server which I'll call www.domain.com/Default.aspx someone typed www.domain.com/Default.aspx/randomstuff
It seems that IIS's response was to serve Default.aspx as normal, but, as far as the browser is concerned, the path is www.domain.com/Default.aspx/ rather than www.domain.com/ and thus all relative paths to CSS, images, etc. fail
I looked at the traffic in Fiddler, and it seems that all of those image etc. requests, such as www.domain.com/Default.aspx/images/image.gif are ALSO returning the contents of Default.aspx, needless to say, not a valid image!
I don't believe they are doing anything special with URL rewriting, but just to be sure, I tried an experiment on a freshly created ASP.NET web application and the results were the same.
So what is causing IIS to pass a URL like /Default.aspx/randomstuff to the ASP.NET pipeline as a request for Default.aspx? And can it be stopped, and made to just throw a 404 as you'd expect?
This is called the PathInfo component.
You can stop like this:
if (!String.IsNullOrEmpty(Request.PathInfo)) throw new HttpException(404);
Yes, that's perfectly normal. Apache will do it too.
You can use it for routing, so you can have the URL /script.name/random/stuff instead of /script.name?page=random&section=stuff without having to set up URL rewriting.
Naturally the browser doesn't know that script.name is the real script, that random and stuff aren't really part of the path at the server-side. So all URLs will be relative to the random directory. Normally when you are writing an application with routing, you have to make sure you use rooted or absolute URLs through rather than relative URLs, for this reason.
And can it be stopped, and made to just throw a 404 as you'd expect?
Yes, as in SLaks's answer. However it would probably be better to send a 301 to the real address without the trailing Path Info parts.

Master Page compilation and Absolute URLs

UPDATED 03/04/09
In response to some comments, a sample from the master page looks like this. This is not an asp.net control, this is hard coded html
<span class="topleft"><span class="bottomleft">About us</span></span>
This renders on the production server as
<span class="topleft"><span class="bottomleft">About us</span></span>
MYDOMAIN is the true domain name of our main site, NEWDOMAIN is a perfectly valid DNS entry which points to the same site.
UPDATED 02/04/09
All the URLS are absolute in the sense that they begin http://
I don't think this can be a browser issue as the actual rendered source code (viewed via view source) has been changed. Checked in both IE7/8 and Firefox 3 and witnessed same behaviour.
Original Question
I have an ASP.Net 2.0 application which has several master pages. This is essentially mocked up to look exactly like our main website, but because it runs on a different server all of the URLs for menu items etc are given absolute URLs to our main site.
This works fine on my development machine, but on the production server all of the URLs which are absolute are changing at runtime, but they still end up at the same pages when clicked.
Is this a DNS issue? Does ASP.Net do some DNS resoltion of URL's when the master page and content are merged? If so then why does it not have the same effect on my local machine, they are on the same domain.
No, it's not a DNS issue, and ASP.net doesn't do any DNS resolution. That's all the responsibility of the browser you're viewing the page in.
However, there are several circumstances that can lead to inconsistent URLs being served in the page's mark-up, which may be interpreted differently by the client's browser.
Browser's will always interpret a URL beginning "http://" the same way - it's an absolute URL, so the destination will always resolve to the same thing. Make sure all your URLs to your main site begin "http://".
URLs beginning "www." (no http://) will be treated as relative URLs - i.e. if the page containing the URL is at http://www.google.com, you're essentially asking for http://www.google.com/YourUrl. You'll find this almost certainly isn't the behaviour you're looking for.
URLs beginning with a leading slash (/) will be treated as absolute on the current domain. For example "/Tools", within Google will result in a request to "http://www.google.com/Tools". If there's no leading slash, the browser will treated the URL as being relative to the page currently being viewed (i.e. a URL of "Tools" when you're viewing a page in the "en" folder would result in a request to "en/Tools".
I think this is where your problems are arising. For consistent behaviour, I find it's a good rule of thumb to ensure all URLs begin with a leading slash. If you want to ensure all your hyperlinks generated by your ASP code are correct, use the tilde (which ASP will replace with the path to the application root folder):
<asp:Hyperlink id="Test1" runat="server" NavigateUrl="~/Tools/Default.aspx">Tools</asp:Hyperlink>
This way, it doesn't matter where your page is in your site structure, whether you're using Cassini, a web site in IIS or a virtual directory in IIS - the URL will always resolve to the correct address.
If you want to output a URL that isn't a property of a server control, use the ResolveUrl method:
Tools
Hope this helps.
By "absolute" I assume you mean they start with a "/" rather than a folder name?
If you are using the ASP.NET hyperlink control, then these will tend to modified to start at the application root.
Edit for comment
Can you give us an example of how the urls are being changed? i.e. From http://www.example.com/somepage.aspx to http://www.example.com/trackingpage.aspx?somequerysting - or is the domain changing? or something else?
You say "they still end up at the same pages" - so clearly things are working. Have you got any HttpHandlers registered in the web.config on your production servers that could be modifying the URLs for you so that they all go through some logging system? I.e. taking the response from the server, processing the resultant HTML, modifying all links - does it happen with simple anchor tags as well as Hyperlink controls?
Are you using a custom base page that is performing additional steps in PreRender or Render that's different on production to your developer machine that is changing the URLs?

Resources