3

If I have a url like "/something/something/" and my site is http://mysite.com and I want to link to that something url, is there a method like Url.Content(); which will discover the virtual directory of the site in IIS and map appropriately to a url path?

I tried Url.GenerateContentUrl(), Url.Action(), Url.Content(), Url.RouteUrl()

3 Answers 3

8

is there a method like Url.Content();

Why like when there is Url.Content?

var url = Url.Content("~/something/something");

which will take care of the virtual directory name and if your side is deployed at the root http://example.com it will return /something/something and if you have a virtual directory http://example.com/applicationname it will return /applicationname/something/something.

So everytime you need to link a static resource you should always use Url.Content. For example:

<img src="<%= Url.Content("~/images/foo.png") %>" alt="" />

will always correctly resolve the image url no matter where your site is deployed to.

Sign up to request clarification or add additional context in comments.

Comments

1

If, for whatever reason, you can't just use Url.Content, you can use either HostingEnvironment.ApplicationVirtualPath or HttpContext.Current.Server.MapPath instead.

But I can't think of a good reason you can't just use Url.Content.

Comments

0

This is what I assume:

You have a file like myfile.jpg on the root on your site. Then you want a url like:

http://mysite.com/myfile.jpg 

Right?

All you need to do is add this in yours routes in Global.asax.cs:

routes.IgnoreRoute("myfile.jpg");

Yes it should work on sub folders too.

4 Comments

No, its a route that links to an action result, so there is no file extension, just a virutal route url. It literally looks like this: "/pagename/actionname/" but there is no controller or action method associated with it, since its just an entry in the routing table.
@Shawn I'm totally unclear about what you want? You want to expose files? Pages? What?
OK then what do you want when the user visits the url?
Server 1 is mapped to ROOT level of IIS (Mysite.com/PageName/RouteValue) Server 2 is in a Virutal Directory (MySite.com/VirtualDir/PageName/RouteValue) so if I hard code <a href="/pagename/routevalue"> it will break on Server 2, and vis versa on server 1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.