gpt4 book ai didi

asp.net-mvc - 获取文件内容的绝对路径

转载 作者:行者123 更新时间:2023-12-02 03:57:57 28 4
gpt4 key购买 nike

在 asp.net mvc View 中是否有任何简单(内置)的方法来获取内容文件夹中文件的绝对路径?

目前我正在使用

@Url.Content("~/Content/images/logo.png")

但是返回的路径不是绝对的。

我知道可以为这种情况构建自己的助手,但我想知道是否有更简单的方法......

最佳答案

这对我有用:

一个 helper :

using System;
using System.Web;
using System.Web.Mvc;

public static class UrlExtensions
{
public static string Content(this UrlHelper urlHelper, string contentPath, bool toAbsolute = false)
{
var path = urlHelper.Content(contentPath);
var url = new Uri(HttpContext.Current.Request.Url, path);

return toAbsolute ? url.AbsoluteUri : path;
}
}

在cshtml中的用法:

@Url.Content("~/Scripts/flot/jquery.flot.menuBar.js", true)

// example output:
// http://example.com/directory/Scripts/flot/jquery.flot.menuBar.js

关于asp.net-mvc - 获取文件内容的绝对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16084081/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com