【问题标题】:How to display the absolute url in ASP.NET Core MVC如何在 ASP.NET Core MVC 中显示绝对 url
【发布时间】:2018-08-08 07:41:12
【问题描述】:

我如何才能获得要在视图上显示的绝对 URL?

我目前在我的代码中有这个:

<link href="~/css/site.min.css" rel="stylesheet" />

..显示如下:

<link href="/css/site.min.css" rel="stylesheet" />

我正在寻找的是以下内容:

<link href="http://www.example.com/css/site.min.css" rel="stylesheet" />

我知道您可以使用较旧的 ASP.NET MVC 5 来做到这一点,但核心版本可以做到这一点吗?

绝对 URL 值得吗,还是相对 URL 就足够了?

【问题讨论】:

  • 在这种情况下,我真的不认为需要完整的 URL。

标签: asp.net asp.net-mvc razor asp.net-core


【解决方案1】:

您应该自己构建 URL。看看this answer,但我认为您的情况不需要绝对网址。

以下是您将在该答案中找到的摘要:

public static string AbsoluteContent(this IUrlHelper url, string contentPath)
{
    HttpRequest request = url.ActionContext.HttpContext.Request;
    return new Uri(new Uri(request.Scheme + "://" + request.Host.Value), url.Content(contentPath)).ToString();
}

使用该辅助方法,您将在视图中编写此代码以获取位于 wwwroot 下的 site.css 的绝对 URL:

@Url.AbsoluteContent("~/site.css")

【讨论】:

    猜你喜欢
    • 2011-07-18
    • 1970-01-01
    • 1970-01-01
    • 2010-09-30
    • 1970-01-01
    • 2021-12-13
    • 2022-01-08
    相关资源
    最近更新 更多