Change "~/foo" to "approot/foo" from inside Global.asax - asp.net

In the global.asax of my MVC app, I have a string like "~/foo". I have to generate a client-side script (JavaScript) and pass the value of this path "~/foo" to that script.
However, before passing that path, I want that the path must resolve to "approot/foo" where approot is the application's root.
So, for e.g. I deploy my application in IIS under a new website named Ding (for want of a better word), the path "~/foo" must resolve to "/Ding/foo".
How do I do this?

You can use the VirtualPathUtility class there's a bunch of methods there to help you.
In your case you want VirtualPathUtility.ToAbsolute().
For example:
var path = VirtualPathUtility.ToAbsolute("~/foo");
Should resolve how you want.

Related

Cannot call Request.PhysicalApplicationPath on VB.NET

I'm trying to request the path of my application to save new files on it, I know I should do something like this:
Dim mypath As String = Request.PhysicalApplicationPath
But for some reason I cannot even find the Request class.
I read it belongs to the System.Web namespace, I added it and still not working. Any ideas?
You can use Server.MapPath("~/logfiles") to get the path of the subdirectory logfiles in the directory where your web pages are.
Is this in an actual ASP.NET web page, or in a handler?
EDIT
Another way is to use Path.Combine(HttpRuntime.AppDomainAppPath, "logfiles")

How can I add location elements programmatically to the web config?

I have an application which creates page routes from a database. My whole site is secured with forms authentication but I need to allow unauthenticated uses to access these routes. I don't want to hard-code <location> tags for the routes in the web.config as this will negate me using a database to generate the routes.
Can anyone help?
Thanks everyone. I've found an answer here
Basically it involves creating a folder for each route and putting a web.config file in it allowing access. This approach needs to be coupled with setting RouteExistingFiles to false so that the routes don't get confused with the folders.
Rather than using strongly typed configuration classes, why not make the modifications directly in XML?
Here's an abbreviated snippet to demonstrate the concept from some code of mine that performance IIS tuning in the machine.config. The principal is the same for other XML config files though. You just need to create the appropriate XPath statements to do what you need.
XmlDocument machineConfigFile = new XmlDocument();
machineConfigFile.Load(MachineConfigPathString);
XmlNode autoConfig = machineConfigFile.SelectSingleNode(#"/configuration/system.web/processModel/#autoConfig");
autoConfig.Value = "false";
machineConfigFile.Save(MachineConfigPathString);
When saved, the XmlDocument object will preserve all other untouched document nodes. Very handy. It works great for modifying the machine.config. The only possible issue I can see is that your application will probably reset when you save your changes to the web.config. So test it out in a safe environment with a backup of your web.config just in case the reset causes any undesired outcomes!
I found this MSDN link for you. I didn't find whether you can modify the config of running server instance this way though.
Have you considered implimenting your site security in a different way? Having a portion of the site that allows unauthenticated access and a portion that does not. I am "assuming" (bad) that you are using MVC since you are describing routes - this is very easy to do with both MVC and traditional web form applications.

Getting ASP.NET root path to Flex?

I am working on a project that is primarily ASP.NET based. The main project is meant to be deployed to multiple locations for different clients, so one client might be located at website.com/client1 and another at website.com/client2. Within the application, we regularly use the application root operator ~ to get the path to a resource.
We also have a bunch of Flex applications that get deployed in there, and many rely on web services within the ASP.NET application. What I'm after is a way to reference the services relative to the application root. Here's an example of the location of some files for two client deployments:
Client A
website.com/clientA/swf/FlexApplication.swf
website.com/clientA/services/webService.asmx
Client B
website.com/clientB/swf/FlexApplication.swf
website.com/clientB/services/webService.asmx
FlexApplication and webService are both exactly the same, so what I want to do is something like this in the Flex code:
var myService:CustomService = new CustomService(~/services/webService.asmx);
myService.callMethod("Test");
I would like to avoid using relative paths for the usual reasons. Is there a good way to do this or a good way to pass the root url to the flex application from ASP.NET? Thanks in advance.
Its definitely a good idea to avoid relative URL's. The easiest way is to pass the information in via Flash Vars in the HTML embed statement.
Check out Adobe's documentation on using FlashVars with Flex: http://livedocs.adobe.com/flex/3/html/help.html?content=passingarguments_3.html
Summary:
Add a line to the HTML embed statement like this:
<param name='flashVars' value='serviceRoot=/myserviceRoot'/>
Then access it in Flex via the mx.core.Application.application.parameters accessor
import mx.core.Application;
var parameters:Object = Application.application.parameters;
var serviceRoot:String = parameters['serviceRoot'];
relative to the application root
Flex knows nothing about the application root of a ASP.NET application. The only thing that the Flex app knows is the URL that it is served from. It does not know the URL of the page. Keep in mind that the URL of the page and the URL of the SWF are not the same.
It appears, given your directory structure, that you can use the SWF's URL to get the information you're after.
In a Flex 3 Application, you can get use the url property of the Application tag:
(Application.application as Application).url
In a Flex 4 Spark application, you can also use the url property of the Application Tag, but you have to get it differently:
(FlexGlobals.topLevelApplication as Application).url
Then you can parse it with URLUtils to get your directory structure. Probably store that as a global variable in your Flex application somehow and use it to construct the URL for the service calls you are making to the remote server.
I use this handy little utility to get URL information via javascript inside the .swf.
http://www.flexpasta.com/index.php/category/utility-classes-that-help-you-deal-with-string-numbers-arrays-and-objects-in-actionscript/

Get path to servlet container deploy/webapps folder

From within the service method of a servlet, I can get the path to the web application's root folder with getServletContext.getRealPath(""), e.g. /tomcat/webapps/MyWebApplication, but is there any way I can get the path to /tomcat/webapps/ or optionally a specific other web application like /tomcat/webapps/MyOtherWebApplication (without resorting to hacks like substracting the context path)?
(Using only TomCat as an example, should work across all servlet containers.)
ServletContext has a getContext(String) method (javadoc) that return the ServletContext corresponding to another webapp, which you could then call getRealPath() on. However, the appserver's security model might forbid this, so use with caution.
Path to Tomcat dir (tomcat/bin):
File inputFile = new File("file.xml"); // tomcat_dir/bin/file.xml
Path to other Tomcat directories, "webapps" for example:
File inputFile = new File("../webapps/file.xml"); // tomcat_dir/webapps/file.xml
Nice way to get rid of the absolute paths :)

Getting web address for local file in ASP.Net

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#?

Resources