ASP.NET MVC Beta和RC升级-在生产环境中确认您的期望和版本号

在开发过程中,作者发现本地ASP.NET MVC应用的CSS表现与部署到主机后的表现不一致。通过浏览器开发者工具和查看源代码,他发现输入类型的差异。经过调查,他发现本地版本较新且从GAC加载,而主机上的版本较旧且本地运行。解决这个问题需要更新NuGet包或手动添加最新版本的引用。这是一个关于版本管理和调试ASP.NET MVC应用的重要教训。
摘要由CSDN通过智能技术生成

I was working on an app locally using a daily build (newer than the currently released one) of ASP.NET MVC and deployed it to a host. I noticed this weird CSS issue. See how the text box on the left running on localhost is small and not styled while the one on the right running on the host is the correct width? You can click on the screenshot if you need to see more.

我正在本地使用ASP.NET MVC的每日内部版本(比当前发布的版本更新)开发应用程序,并将其部署到主机上。 我注意到了这个奇怪CSS问题。 看看左侧在localhost上运行的文本框如何小而没有样式,而右侧在主机上运行的文本框是正确的宽度? 如果需要查看更多信息,可以单击屏幕截图。

I dug around a little and using the F12 browser developer tools (as well as good old View Source) I noticed the HTML being created was different! My local app was producing one bit of markup, then when I deployed the same app to my host I would get different markup!

我仔细研究了一下,然后使用F12浏览器开发人员工具(以及良好的旧版View Source),发现所创建HTML是不同的! 我的本地应用程序产生了一点标记,然后当我将同一应用程序部署到主机时,我将获得不同的标记!

Here's the app's markup running locally:

这是在本地运行的应用程序的标记:

<input class="text-box single-line" data-val="true" 
data-val-date="The field EventDate must be a date."
data-val-required="The EventDate field is required."
id="EventDate" name="EventDate" type="datetime" value="5/29/2012 1:48:23 AM" />

Here's while running on the host:

在主机上运行时:

<input class="text-box single-line" data-val="true" 
data-val-date="The field EventDate must be a date."
data-val-required="The EventDate field is required."
id="EventDate" name="EventDate" type="text" value="5/29/2012 1:48:23 AM" />

I realize I could have made it more obvious for this but I wanted to make the point that it took a second to notice that my standard ASP.NET MVC Helper call...

我意识到我可以对此做得更明显,但是我想指出的是,花了一秒钟才注意到我的标准ASP.NET MVC Helper调用...

<div class="editor-field">
@Html.EditorFor(model => model.EventDate)
@Html.ValidationMessageFor(model => model.EventDate)
</div>

...was returning type="text" on the host but type="datetime" on my local machine and that difference was picked up CSS that targeted specific input tags. Weird. Now, EditorFor() can have its behavior overridden with custom files in ~\Shared\EditorTemplates so if there was a DateTime.cshtml in there that would make sense. There wasn't. I wasn't using the very lovely MvcHtml5Templates NuGet package by Scott Kirkland, so that wasn't it.

...正在主机上返回type =“ text”,但在我的本地计算机上返回了=“ datetime”,因此区别在于针对特定输入标签CSS。 奇怪的。 现在,EditorFor()的行为可以被〜\ Shared \ EditorTemplates中的自定义文件覆盖,因此,如果其中有一个DateTime.cshtml有意义的话。 没有。 我没有使用Scott Kirkland的非常可爱的MvcHtml5Templates NuGet包,不是

My spider-sense was telling me this must be a versioning issue but everything looked right. My only explanation was that somehow what was running on the host was different from what was running on my local machine.

我的蜘蛛侠告诉我这一定是版本问题,但一切看起来都不错。 我唯一的解释是,主机上运行的内容与本地计算机上运行的内容有所不同。

I asked around work and Eilon Lipton suggested I run this snippet to check the version of ASP.NET MVC. He's basically saying "Hey, where did this well known type come from?"

我询问了周围的工作, Eilon Lipton建议我运行此代码段来检查ASP.NET MVC的版本。 他基本上是在说:“嘿,这种众所周知的类型是从哪里来的?

public ActionResult ShowVersion()
{
Type t = null;
try
{
t = Type.GetType("System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35");
}
catch (Exception e)
{
Response.Write("Error finding MVC: " + e.ToString());

}
if (t == null)
{
Response.Write("Can't find MVC");
}
else
{
Response.Write("Found MVC at " + t.Assembly.CodeBase + ", IsInGac = " + t.Assembly.GlobalAssemblyCache + "<br>");
var verAttr = t.Assembly.GetCustomAttributes(typeof(System.Reflection.AssemblyFileVersionAttribute), true)[0] as System.Reflection.AssemblyFileVersionAttribute;
Response.Write("Version = " + verAttr.Version + "<br>");

}
return null;
}

Even better, rather than using this code, I can use the MvcDiagnostics NuGet Package and get THIS useful data from Brad Wilson. Hopefully he will update it for ASP.NET MVC 4 although it worked well.

甚至更好的是,我可以使用MvcDiagnostics NuGet软件包而不是使用此代码,并从Brad Wilson获得有用的数据。 希望他会为ASP.NET MVC 4更新它,尽管它运行良好。

This package gives you lots of useful information for debugging weird situations.

该软件包为您提供了许多调试怪异情况的有用信息。

Anyway, I put the diagnostic code in a controller and ran it and got this (this version is faked):

无论如何,我将诊断代码放在控制器中并运行它并得到了此消息(该版本是伪造的):

Found MVC at file:///C:/mysite/bin/System.Web.Mvc.DLL, IsInGac = False
Version = 4.0.77777.0

Hang on, but my local result was this:

等等,但是我的本地结果是这样的:

Found MVC at file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Web.Mvc/v4.0_4.0.0.0__31bf3856ad364e35/System.Web.Mvc.dll, IsInGac = True
Version = 4.0.99999.0

My local version was newer and was running from the GAC (Global Assembly Cache) but the hosted version was older and running locally. That means I had older ASP.NET MVC files floating around that my project referenced and deployed to the host.

我的本地版本较新,并且从GAC(全局程序集缓存)运行,但是托管版本较旧并且在本地运行。 这意味着我有较旧的ASP.NET MVC文件在我的项目所引用并部署到主机的周围浮动。

Aside: The specific change that "got" me here was actually the first external contribution accepted to ASP.NET MVC. It's the one that Miguel de Icaza sent a pull request for while I was on stage at DevConnections. The change is to output HTML5 input types for common default data types, like <input type="datetime"> for System.DateTime when using EditorFor().

撇开:这里“让我知道”的特定更改实际上是ASP.NET MVC接受的第一个外部贡献 这是我在DevConnections上台时Miguel de Icaza发送的拉动请求。 所做的更改是为常见的默认数据类型输出HTML5输入类型,例如在使用EditorFor()时为System.DateTime的<input type =“ datetime”>。

It took me a moment to remember that I hadn't added the reference to ASP.NET MVC manually but rather via the NuGet Package Manager. This became crystal clear when I used the built-in NuGet Package Visualizer (from Tools | Library Package Manager) to see the package dependency chain:

我花了一些时间来记住,我没有手动将引用添加到ASP.NET MVC,而是通过NuGet包管理器添加了。 当我使用内置的NuGet包可视化程序(来自“工具” |“库包管理器”)来查看包依赖关系链时,这一点变得非常清晰:

The ASP.NET NuGet Packages as viewed in a directed graph in the NuGet Package Visualizer

Basically I needed to either update my out of date NuGet packages or add the assembly references manually without NuGet.

基本上,我需要更新我的过时的NuGet软件包,或者在没有NuGet的情况下手动添加程序集引用。

Since I was working on a daily build I did it manually. When you update your ASP.NET MVC 4 Beta applications to a newer build (like ASP.NET MVC 4 Release Candidate (whenever that comes out) or the final Release) you'll want to do update all your packages via NuGet and confirm you're getting the versions and behavior you expect. The NuGet Package Visualizer is a hidden gem indeed.

由于我从事的是日常构建,因此我手动进行了构建。 当将ASP.NET MVC 4 Beta应用程序更新到较新的版本(例如ASP.NET MVC 4 Release Candidate(无论何时发行)或最终版本)时,您将希望通过NuGet更新所有软件包并确认您正在获得您期望的版本和行为。 NuGet包可视化工具确实是一个隐藏的宝石。

So, a general reminder to folks (and a specific lesson when upgrading between ASP.NET MVC builds):

因此,向人们发出一般性的提醒(以及在ASP.NET MVC构建之间进行升级时的一课):

  • know your version numbers, what you want, what you're referencing.

    知道您的版本号,所需内容,所引用的内容。
  • confirm your version numbers in your debug and production environment.

    在调试和生产环境中确认您的版本号。

    • Perhaps a health-check page or a runnable Integration Test to assert your assumptions

      可能运行状况检查页或可运行的集成测试来断言您的假设

  • update your NuGet packages keep your references, and thus your ~\bin up to date

    更新您的NuGet软件包会保留您的引用,因此您的〜\ bin是最新的
  • know where your assemblies are being loaded from (the GAC or locally)

    了解从哪里(GAC或本地)加载程序集

Hope this helps!

希望这可以帮助!

翻译自: https://www.hanselman.com/blog/aspnet-mvc-beta-and-rc-upgrades-confirm-your-expectations-and-version-numbers-in-production-environments

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值