Omu
Omu

Reputation: 71206

How to get the value of ~ in an mvc view?

I need to get the absolute path of the root in an asp.net mvc view

I guess one way would be to do Url.Action("Index","Home") and will get http://localhost/myapp

anybody knows the right way ?

Upvotes: 2

Views: 110

Answers (2)

stevemegson
stevemegson

Reputation: 12093

Url.Content() will give you that. It's intended to allow you to get the path for static files, such as

<img src='<%: Url.Content("~/images/logo.gif") %>' />

Calling Url.Content("~") will return /myapp/, or just / if your application isn't in a virtual directory.

Upvotes: 3

Lorenzo
Lorenzo

Reputation: 29427

I think you can use

string urlBase = Request.Url.GetLeftPart( UriPartial.Authority ) + Request.ApplicationPath;

Upvotes: 0

Related Questions