加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc-3 – 在chtml页面中使用UrlHelper的扩展而不引用

发布时间:2020-12-16 06:46:23 所属栏目:asp.Net 来源:网络整理
导读:我有一个UrlHelper的扩展,我将在每个页面上使用chtml.我是否有任何方法可以参考此扩展而无需使用它? UrlExtender.cs using System;using System.IO;using System.Web.Mvc;namespace MySite.Web.MVC.Extender{ public static class UrlExtender { public sta
我有一个UrlHelper的扩展,我将在每个页面上使用chtml.我是否有任何方法可以参考此扩展而无需使用它?

UrlExtender.cs

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

namespace MySite.Web.MVC.Extender
{
    public static class UrlExtender
    {
        public static string ContentLastWrite(this UrlHelper helper,string contentPath)
        {
            try
            {
                DateTime lastWriteTime = (new FileInfo(helper.RequestContext.HttpContext.Server.MapPath(contentPath))).LastWriteTime;
                contentPath = string.Format("{0}?v={1:yyyyMMddHHmmss}",contentPath,lastWriteTime);

                return helper.Content(contentPath);
            }
            catch
            {
                return helper.Content(contentPath);
            }
        }
    }
}

page.chtml

@using MySite.Web.MVC.Extender


<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
    <link href="@Url.ContentLastWrite("~/Content/Site.css")" rel="stylesheet" type="text/css" />
...

我想避免在每个页面上调用“MySite.Web.MVC.Extender”“chtml”

谢谢

解决方法

添加< add namespace =“MySite.Web.MVC.Extender”/>到< system.web.webPages.razor> /< pages> /< namespaces>在Views / Web.config中.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读