简体   繁体   中英

How do I get project information while in an mvc controller?

How do I get the site's version numbers [Major].[Minor].[Revision].[Build] at runtime in the HomeController.cs? I plan to display that information in the footer of my website...

You could get the version of the current assembly that is hosting your controller:

var version = Assembly.GetExecutingAssembly().GetName().Version;

But if you are going to display this on the footer of your website, you'd rather write a custom HTML helper instead of polluting all your controllers with this:

public static class HtmlExtensions
{
    public static IHtmlString SiteVersion(this HtmlHelper html)
    {
        var version = typeof(HtmlExtensions).Assembly.GetName().Version;
        return new MvcHtmlString(string.Format("[{0}].[{1}].[{2}].[{3}]", version.Major, version.Minor, version.Revision, version.Build));
    }
}

and then in your footer:

@Html.SiteVersion()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM