gpt4 book ai didi

.net - ASP.NET MVC Web 应用程序中忽略了 AssemblyInfo 版本控制?

转载 作者:行者123 更新时间:2023-12-02 13:48:01 24 4
gpt4 key购买 nike

这里很奇怪。根据 AssemblyInfo.cs 中的设置,我的 MVC Web 应用程序的版本号未正确打印到我的 View 中。我在 AssemblyInfo.cs 集中设置的定义是“1.0.232.0”。

我尝试了多种方法来打印它:

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

(结果0.0.0.0)

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

(结果为 2.0.0.0,它在我的项目中没有设置。)

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

(结果为 2.0.0.0)

这让我相信它根本就不能获取我的 AssemblyInfo.cs 文件?如果我尝试使用“发布”按钮发布到我们的开发服务器上的 IIS,也会出现这种情况。

有什么想法吗?也许我使用了错误的语句来获取版本号? :\

谢谢大家。

最佳答案

View (默认情况下)仍然很大程度上是按需编译的,因此您无法在 View 中可靠地使用 GetExecutingAssembly() - 但是,对我来说, Controller 使用效果很好:

[assembly: AssemblyVersion("1.2.3.4")]

与:

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

在页面中显示

1.2.3.4

编辑您犯的错误是调用 typeof(...).GetType() - 这将为您提供 Type (或子类) - 所以是的,它将是2.x。/编辑

有关预编译 View 的额外步骤,请参阅“用于编译 View 的 MSBuild 任务”here .

可以说,你的 View 本身不应该获取这些数据 - 它应该被放入 ViewData (或类似的)中,也许通过基本 Controller 或操作过滤器。

<小时/>

关于母版页的问题;首先,选择一个键;-p

<%=ViewData["AppVersion"] %>

然后我想到了两个选项:在 Controller (或公共(public)基本 Controller )中覆盖 OnActionExecuting:

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

或者创建一个 Action 过滤器:

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

并使用此属性标记您的 Controller (类)或操作(方法):

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

关于.net - ASP.NET MVC Web 应用程序中忽略了 AssemblyInfo 版本控制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/672327/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com