NOCARRIER
NOCARRIER

Reputation: 2633

MVC Url helper method for static pages?

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()

Upvotes: 3

Views: 3652

Answers (3)

Rup
Rup

Reputation: 34418

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.

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1039508

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.

Upvotes: 8

gideon
gideon

Reputation: 19465

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.

Upvotes: 0

Related Questions