4

我有一个 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" />
...

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

谢谢

4

1 回答 1

6

添加<add namespace="MySite.Web.MVC.Extender" />到. <system.web.webPages.razor>/<pages>/<namespaces>_Views/Web.config

于 2011-10-12T19:47:59.803 回答