Can't get site root url in asp mvc

前端 未结 5 1225
伪装坚强ぢ
伪装坚强ぢ 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:36

    The @Url.Content is returning the relative URL which is correct. The following code gives you various option in getting a URL realtive via code, absolute Url via code, absolute via javascript:

    Site 'Relative Url = '@Url.Content("~")'
    Site 'Absolute Url = '@System.Web.HttpContext.Current.Request.Url.AbsoluteUri'

    You will get something like:

    Site 'Relative Url = '/' Site 'Absolute Url = 'http://localhost:14763/'

    and also the JS alert showing the href/absolute Url

提交回复
热议问题