读取 ASP.NET 中的 AssemblyTitle 属性
我使用下面的代码来读取.NET应用程序的AssemblyTitle属性,不幸的是Assembly.GetEntryAssembly()在ASP.NET应用程序中总是返回Null。如何在 ASP.NET 应用程序中读取 AssemblyTitle?
public static string Title
{
get
{
var attributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
if (attributes.Length > 0)
{
var titleAttribute = (AssemblyTitleAttribute)attributes[0];
if (titleAttribute.Title.Length > 0)
return titleAttribute.Title;
}
return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().CodeBase);
}
}
I use code below to read AssemblyTitle attribute of .NET apps, unfortunately Assembly.GetEntryAssembly() always return Null in ASP.NET app. How to read AssemblyTitle in ASP.NET app?
public static string Title
{
get
{
var attributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
if (attributes.Length > 0)
{
var titleAttribute = (AssemblyTitleAttribute)attributes[0];
if (titleAttribute.Title.Length > 0)
return titleAttribute.Title;
}
return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().CodeBase);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须有一个已知类型是在包含
AssemblyTitle
的同一程序集中定义的。然后你可以这样做:请注意(据我所知)没有任何其他防弹方法。
例如,如果您不想在 Web 请求期间执行此操作,则使用 HttpContext.Current 不起作用(因此您可以在响应用户操作时执行此操作,但不能从单独的线程或从静态初始化器,或来自
global.asax
)一些类似的读物(成功了一半):
适用于 Web 应用程序的 GetEntryAssembly
使用来自程序集(ASP.NET/C#)
You must have a type that you know is defined in the same assembly that contains the
AssemblyTitle
. Then you can do:Note that (for what I know) there isn't any other bulletproof method.
For example using
HttpContext.Current
doesn't work if you want to do it not during a web request (so you can do it on response of a user action, but not from a separate thread, or from a static initializer, or fromglobal.asax
)Some similar readings (full of half successes):
GetEntryAssembly for web applications
Using the Web Application version number from an assembly (ASP.NET/C#)
我在 asp.net web 应用程序中使用以下内容:
编辑:抱歉,这只是版本,而不是标题!我将你的版本和我的版本结合起来:
这样就可以很好地获取程序集标题属性。区别在于
GetExecutingAssembly()
与GetEntryAssembly()
。I use the following in asp.net web app:
Edit: Sorry, thats just the version, not the title! I combined your version and mine:
That gets the assembly title attribute just fine. The difference is in
GetExecutingAssembly()
versus yourGetEntryAssembly()
.