获取内容文件的绝对路径 [英] Get absolute path of file on content

查看:94
本文介绍了获取内容文件的绝对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个asp.net mvc的观点没有简单的(内置)的方式来获得在内容文件夹中的文件的绝对路径?

Is there any easy (built in) way in an asp.net mvc view to get the absolute path of a file in the content folder?

目前我使用

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

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

But the path returned isn't absolute.

我知道这是不可能建立自己的帮手这样的情况下,但我想知道是否有任何更简单的方法...

I know it is possible to build its own helper for such cases but I'd like to know if there's any easier way...

推荐答案

这对我的作品:

一个帮手:

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)

这篇关于获取内容文件的绝对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆