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

在asp.net mvc视图中是否有任何简单(内置)方式获取内容文件夹中文件绝对路径

在我正在使用的那一刻

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

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

我知道有可能为这种情况建立自己的帮手,但我想知道是否有更简单的方法

解决方法

这对我有用:

助手:

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

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....