Can't get site root url in asp mvc

前端 未结 5 1232
伪装坚强ぢ
伪装坚强ぢ 2020-12-30 21:13

I need to get site root url in razor page in javascript code:

...
var siteRootUrl = \'@Url.Content(\"~\")\';
...

But all I get from this is

5条回答
  •  忘掉有多难
    2020-12-30 21:22

    To get the current host with port (mysite.com, www.mysite.com or localhost:9876)

     Request.Url.Authority
    

    To get your current application folder: (/ or /appfolder/)

     Url.Content("~/")
    

    To mix them?

     String.Format("{0}://{1}{2}",Request.Url.Scheme, Request.Url.Authority,Url.Content("~/"))
    

    OR (As torm pointed out)

     Url.Action("", null, null, Request.Url.Scheme)
     Url.Action("", null, null, "http")
     Url.Action("", null, null, "https") 
    

    To generate an Action URL:

     Url.Action("About","Home",null,"http")
    

提交回复
热议问题