Question

I have an extension of UrlHelper, I'll use on every page chtml. Do I have any way to refer to this extension without having to do it by using?

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" />
...

I want to avoid calling "MySite.Web.MVC.Extender" on every page "chtml"

Thanks

Était-ce utile?

La solution

Add <add namespace="MySite.Web.MVC.Extender" /> to <system.web.webPages.razor>/<pages>/<namespaces> in Views/Web.config.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top