在每个视图上运行操作方法

时间:2014-05-30 06:50:42

标签: asp.net razor asp.net-mvc-5

我想在每个页面上显示当前项目的版本信息。

我现在这样做的方式是这样的:

在我的homecontroller的Index方法中:

ViewBag.Version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;

在_layout.cshtml(母版页)中:

@ViewBag.Version

这里的问题是,它只会显示一次,但我想在每个页面/视图上显示版本。

这是我的路由配置:

        public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }


        );
    }

我必须在这里改变一些东西吗?

Ty求助

2 个答案:

答案 0 :(得分:1)

为什么不打电话

@System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
<{1>}视图上的

MVC有一个不同的启动上下文,需要一个解决方法来发布版本:

_Layout.cshtml

@typeof(HomeController).Assembly.GetName().Version 可以替换为MVC应用程序集的程序集中的任何其他类型。有关详细信息,请参阅this question

答案 1 :(得分:0)

把它放在布局中。

<label>@System.Reflection.Assembly.GetExecutingAssembly().GetName().Version</label>
相关问题