在ASP.NET MVC Web应用程序集信息版本控制忽略? [英] AssemblyInfo versioning ignored in ASP.NET MVC Web App?

查看:349
本文介绍了在ASP.NET MVC Web应用程序集信息版本控制忽略?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

奇怪的人在这里。根据是什么在的AssemblyInfo.cs 设置我的MVC Web应用程序的版本号是不正确打印我的看法。该定义我的设置设定为的AssemblyInfo.cs 1.0.232.0

Strange one here. My MVC Web Application's version number is not printing correctly to my view according to what is set in AssemblyInfo.cs. The definition I have set in set AssemblyInfo.cs is '1.0.232.0'.

我试图以打印多种方法:

I have tried multiple methods in order to print it:

<%= System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()%>

(结果0.0.0.0)

(results 0.0.0.0)

<%= System.Reflection.Assembly.GetCallingAssembly().GetName().Version.ToString()%>

(结果2.0.0.0,这是设置无处在我的项目。)

(results in 2.0.0.0, which is set nowhere in my project.)

<%= typeof(HomeController).GetType().Assembly.GetName().Version.ToString()%>

(结果2.0.0.0)

(results in 2.0.0.0)

这使我相信,那根本不能拿起我的AssemblyInfo.cs文件?这也是这种情况,如果我尝试使用发布按钮发布到我们的IIS开发服务器上。

This leads me to believe that it simply must not be picking up my AssemblyInfo.cs file? This is also the case if I attempt to use the "Publish" button to publish to IIS on our development server.

任何想法?也许我使用错误的语句来获取版本号? :\

Any ideas? Perhaps I'm using the wrong statement to fetch the version number? :\

谢谢你们。

推荐答案

的看法是(默认)在很大程度上仍然编译点播,让您可靠续使用 GetExecutingAssembly()在视图中 - 但是,对我来说,控制器使用正常工作:

The view is (by default) still largely compiled on-demand, so you con't reliably use GetExecutingAssembly() within the view - however, for me the controller usage works fine:

[assembly: AssemblyVersion("1.2.3.4")]

<h2><%=typeof(MvcApplication4.Controllers.HomeController).Assembly
   .GetName().Version.ToString() %></h2>

节目

1.2.3.4

在页面中。

修改 你犯的错误是调用的typeof(...)的GetType() - 这是要给你键入 (或子类) - 所以是的,这将是2.x的 /修改

edit The mistake you made was calling typeof(...).GetType() - that is going to give you Type (or a subclass) - so yes, it will be 2.x. /edit

有关的额外步骤pre-编译的意见,请参阅MSBuild任务的编译次数<一href="http://weblogs.asp.net/scottgu/archive/2008/12/19/asp-net-mvc-design-gallery-and-upcoming-view-improvements-with-the-asp-net-mvc-release-candidate.aspx"相对=nofollow>这里。

For the extra step of pre-compiling the views, see "MSBuild Task for Compiling Views" here.

可以说,你的看法不应该反正取这个数据本身 - 它应该被放入ViewData的(或类似),或许是一个基控制器或动作过滤

Arguably, your view shouldn't be fetching this data itself anyway - it should be put into the ViewData (or similar), perhaps by a base-controller or action-filter.


再约母版页的问题;第一,选择一个关键;-p

Re the question about master pages; first, pick a key ;-p

<%=ViewData["AppVersion"] %>

然后两个选项飞跃在脑海中:覆盖 OnActionExecuting 控制器(或一个共同的基础控制器):

then two options leap to mind: override OnActionExecuting in the controller (or a common base-controller):

    protected override void OnActionExecuting(
        ActionExecutingContext filterContext)
    {
        filterContext.Controller.ViewData["AppVersion"] =
            GetType().Assembly.GetName()
            .Version.ToString(); // probably cached
        base.OnActionExecuting(filterContext);
    }

,或创建一个动作过滤器:

or create an action-filter:

public class AppVersionAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(
         ActionExecutingContext filterContext)
    {
        filterContext.Controller.ViewData["AppVersion"] =
            GetType().Assembly.GetName()
            .Version.ToString(); // probably cached
        base.OnActionExecuting(filterContext);
    }
}

和与此属性标记您的控制器(类)或动作(方法):

And mark your controllers (classes) or actions (methods) with this attribute:

[HandleError, AppVersion]
public class HomeController : Controller
{ ... }

这篇关于在ASP.NET MVC Web应用程序集信息版本控制忽略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆