开发者

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 appropri开发者_StackOverflow中文版ately to a url path?

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


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜