Is there a 'correct' way to get the proper web address for a file under an ASP.Net application? For example, I have content in '/Content/Images/Gallery/2010-01-17/small/', and I would like to iterate through all of those files, and output to the browser a link.
Now, I can do it manually by working out the path from the files FullName or I can do it from knowing the current directory, but is there a proper ASP.Net way to do it?
As you can probably tell, I'd rather use the provided method if it exists :)
Regards
Moo
You can use the method ResolveUrl() for that. If your content directory is located directly under you web app's root directory, then this should work:
// "~" results in an URL to your web app's root directory
string imageBaseUrl = this.ResolveUrl("~/content/gallery/2010-01-17/small");
Then you can append the names of the images to that base URL.
I think ResolveUrl is only part of the answer.
Unfortunately, there is not a built-in function to return a full URL to a particular resource, inclusive of hostname and protocol. Part of the reason for this is that you can access a URL any number of ways... and the server is completely agnostic of the hostname. You have to look at either the Request.Url properties to build a new URL from the user's request, or use ServerVariables.
See this question:
How to Convert "~/default.aspx" to "http://www.website.com/default.aspx" C#?
Related
I am using the rather excellent IIS7 Rewrite module (V2), and want to create a custom RewriteProvider that rewrites differently depeneding on whether the physical file exists.
I have successfully created a provider, as in this tutorial:
http://learn.iis.net/page.aspx/804/developing-a-custom-rewrite-provider-for-url-rewrite-module/
However, really need to be able to map the url to a physical path - I would normally do this via HttpContext.Current.Server.MapPath() or HostingEnvironment.MapPath(), but it looks like the HttpContext has not been initialised (at least within the current App Domain - since the ReWrite module is native code, I'm having difficulty working out where I can get this information).
I don't really want to have to resort to creating my own rewrite module to get around this problem - anyone have any clues for me?
Thanks!
Mark.
You will not be able to get to it using those APIs since the code runs in a different AppDomain than the one ASP.NET is using.
The only way I can think to make this work is to pass the right Server Variable that includes the physical path to your extension and do a Path.Combine() yourself.
So assuming you have an extension called YourProvider that you are calling somehow like this:
{YourProvder:{URL}}
You can do:
{YourProvder:{APPL_PHYSICAL_PATH}|{URL}}
You can now get the physical path and the URL separated by a pipe | , make sure to pass the Physical Path first since the URL is in the control of external users you do not want them to trick you into getting a different physical path.
I've got an HTTP service I defined in Flash Builder, via the "Data Services" tab. I've got an absolute URL in there right now.
What I really want is to not define a path that includes a domain name at all--I want the service to simply call an absolute path that's on the same domain as whatever domain the SWF was served from... can I do that? When I got rid of the base URL and then gave an absolute URL path (e.g., /roster/deleteMember), Flex Builder complained that "File does not exist." Well, of course it doesn't exist, it isn't a file, it's a URL to a service call--there is no corresponding file on the filesystem.
Can anyone advise me how to do that? If I change the domain name, it wipes out all the parameter definitions for the methods, so I have to go back to each method and setup the parameters again. Rather a headache.
Now, I would have thought this would work. from the adobe documentation:
The configuration files sometimes contain special {server.name} and {server.port} tokens. These tokens are replaced with server name and port values based on the URL from which the SWF file is served when it is accessed through a web browser from a web server. Similarly, a special {context.root} token is replaced with the actual context root of a web application.
So, if you specify the endpoint as
http://{server.name}:{server.port}/{context.root}, then
automatically on runtime, the variables are set by the flashplayer depending on where you've been downloaded the application.
Sounds great... but it's not working for me. I can't even set those values in the Flash Builder Data Services tool. Here's a recording of what I'm getting.
http://screencast.com/t/MTk0NzNiYzY
I'm not sure it's possible from the DS window.
If you were doing it in code, you could use Application.application.url to get where the swf had been loaded from.
I am encountering an unexpected behavior:
The following statement works fine:
Context.RewritePath( "~/Default.aspx" ); // redirect to default doc, explicitly
This gives me a 404 error:
Context.RewritePath( "~/" ); // redirect to default doc, implicitly
Loading document / from a browser without doing any URL rewriting correctly loads the document, so I figure IIS is correctly configured, and that / and /Default.aspx indeed refer to the same document.
I would rather use the latter statement, as there is a possibility that the Default document name will be changed in IIS as time goes on. I'm assuming the solution involves some method to retrieve the Default Document name from IIS, however I've been unable to locate such a method.
So my question is: What is the correct way to specify a default document when rewriting the URL?
Your problem is that IIS handles the path translations for the "default document" before it turns control over to asp.net.
When a browser requests a URL without a file name, IIS will check the list of "default documents" configured for that site. It then looks for physical files in the requested path that match the name of the configured default documents. It then returns the first matching default document that physically exists on the disk.
After this, if the requested file is an asp.net file, it will invoke the asp.net runtime and hand off processing to asp.net.
Your URL re-writing takes place inside the asp.net process. It has no awareness of IIS's settings with regards to default documents and such. When you use a technique like URL re-writing that take place entirely within asp.net, you can't use default documents and such. So always re-write using the page name.
Is it possible to programmatically resolve a URL to a file using ASP.NET and IIS? Specifically I'd like the file to be outside of my Virtual Directory (could be anywhere on the local file system). So if a URL comes in like http://mysite/somepicture.jpg I'd like to be able to return c:\mypicture.jpg. I looked into creating an IHttpModule for URL rewriting but that isn't quite what I need - it's limited to URLs within the existing site.
You cannot achieve it by URL rewriting as the file is not hosted on your Web site. You should use Response.WriteFile method in an HttpModule or HttpHandler to manually stream the file to the user.
I would like to add to Mehrdad's response by saying that you need to make sure your app has rights to the folder the files you want live in. That way you can dish it out as Mehrdad suggested.
If I change the url in the web.config file will the change be reflected in the .disco, .discomap, and .wsdl files that are in the WebReferences folder?
[Edit]
I'm using asp.net 2005
The change will not, as far as I'm aware, be reflected in all the other files. However, if you change the URL in web.config, your application will call the web service from the new URL at run-time. Check out this blog entry. (No, it's not one of mine!)
Assuming you mean in the client, set the Url property at execution time. You can configure this from anywhere you want, so long as you have access to the value at the appropriate time.
As I can remember WSDL file contains the binding(s) (URL mappings) of your WS. So if you change the URL on your machine (in WSDL, Disco, etc.) that's enough.
On the other hand don't forget to regenerate client proxies. They have to reflect WSDL changes.