如何获取<system.webServer><httpErrors>web.config 中元素中设置的errorMode属性的值?
我正在尝试在ASP.NET Web应用程序中实现一些"自我诊断".当应用程序启动时,它会运行web.config中的某些设置并确认它们已正确设置.
虽然在<system.web><customErrors>元素中设置了errormode时这段代码工作得很好,
var errSec = (CustomErrorsSection)HttpContext.Current.GetSection("system.web/customErrors");
Response.Write(errSec.Mode.ToString());
Run Code Online (Sandbox Code Playgroud)
一旦在IIS7上部署站点并且现在找到此设置,它将无法工作system.webServer -> httpErrors.
这不起作用:
var errSec = (CustomErrorsSection)HttpContext.Current.GetSection("system.webServer/httpErrors");
Run Code Online (Sandbox Code Playgroud)
而铸造到一个CustomErrorsSection也似乎是一个坏主意,必须有一个更好的类型使用?
我在IIS.NET,HTTP错误 上发现了这篇文章,但我希望这样做而不依赖于Microsoft.Web.Administration库.
有什么建议??
UPDATE
好的,根据下面的建议,我试过这个:
var errSec = (ConfigurationSection)HttpContext.Current.GetSection("system.webServer/httpErrors");
Response.Write(errSec.SectionInformation.GetRawXml().ToString());
Run Code Online (Sandbox Code Playgroud)
但这也不起作用,该errSec对象为null.另外,如果我<system.web><customErrors>使用相同的方法加载该部分,则GetRawXml()方法调用将失败并显示"此操作在运行时不适用".异常消息.
我知道如何将整个web.config作为xml文件加载并查询以获取我需要的元素.但在我看来,似乎必须有一个更优雅的方法.
如何将web.config读取为xml:
var conf = XDocument.Load(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + "web.config");
var errMode = conf.Root.Element("system.webServer").Element("httpErrors").Attribute("errorMode").Value;
Run Code Online (Sandbox Code Playgroud)
......但这只是令人讨厌的!如果在machine.config或类似设置中设置了errorMode设置,它将无法工作.
我如何在Razor(VB.NET)中执行此操作:
<div class="some_prefix_@ViewBag.MyValue">
Run Code Online (Sandbox Code Playgroud)
如果@ViewBag.MyValue是"xxx",则输出应为
<div class="some_prefix_xxx">
Run Code Online (Sandbox Code Playgroud) 这是我的问题的简化示例.我有一个表,其中有一个带有重复条目的"名称"列:
ID Name
--- ----
1 AAA
2 AAA
3 AAA
4 BBB
5 CCC
6 CCC
7 DDD
8 DDD
9 DDD
10 DDD
Run Code Online (Sandbox Code Playgroud)
像这样做的SELECT Name, COUNT(*) AS [Count] FROM Table GROUP BY Name结果如下:
Name Count
---- -----
AAA 3
BBB 1
CCC 2
DDD 4
Run Code Online (Sandbox Code Playgroud)
我只关心重复项,所以我将添加一个HAVING子句,SELECT Name, COUNT(*) AS [Count] FROM Table GROUP BY Name HAVING COUNT(*) > 1:
Name Count
---- -----
AAA 3
CCC 2
DDD 4
Run Code Online (Sandbox Code Playgroud)
到目前为止琐碎,但现在事情变得棘手:我需要一个查询来获取所有重复记录,但在Name列中添加了一个很好的递增指示符.结果应如下所示:
ID Name
--- -------- …Run Code Online (Sandbox Code Playgroud) 我有一个具有相当慢的搜索功能的ASP.NET站点,我想通过使用查询作为缓存键将结果添加到缓存一小时来提高性能:
using System;
using System.Web;
using System.Web.Caching;
public class Search
{
private static object _cacheLock = new object();
public static string DoSearch(string query)
{
string results = "";
if (HttpContext.Current.Cache[query] == null)
{
lock (_cacheLock)
{
if (HttpContext.Current.Cache[query] == null)
{
results = GetResultsFromSlowDb(query);
HttpContext.Current.Cache.Add(query, results, null, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
}
else
{
results = HttpContext.Current.Cache[query].ToString();
}
}
}
else
{
results = HttpContext.Current.Cache[query].ToString();
}
return results;
}
private static string GetResultsFromSlowDb(string query)
{
return "Hello World!";
}
} …Run Code Online (Sandbox Code Playgroud) 我正在动态地向页面添加Web用户控件.使用LoadControl仅采用指向.ascx工作的虚拟路径的方法非常好.然而,过载LoadControl需要一个类型和一个参数数组引起了一些令人头疼的问题.
Web用户控件按预期实例化,但Web用户控件中包含的控件为null,一旦我尝试使用它们就会出现异常.很奇怪,因为它在使用第一版时起作用LoadControl.
Web用户控件,简单,带Literal控件:
<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="MyControl.ascx.vb" Inherits="MyControl" %>
<asp:Literal ID="myLiteral" runat="server"></asp:Literal>
Run Code Online (Sandbox Code Playgroud)
控件的代码背后:
Public Class MyControl
Inherits System.Web.UI.UserControl
Public Property Data As MyData
Public Sub New()
End Sub
Public Sub New(data As MyData)
Me.Data = data
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
myLiteral.Text = Data.ID ' The Literal is null, but ONLY when I use the second LoadControl() method!
End Sub …Run Code Online (Sandbox Code Playgroud) 小问题,但它仍然让我疯狂.
我正在构建一个<script>包含要在ASP.NET页面上呈现的标记的URL ,如下所示:
<script src='<%= string.Format("http://example.com/page.aspx?a={0}&b={1}&c={2:0.00}", A, B, C)%>' type='text/javascript'></script>
Run Code Online (Sandbox Code Playgroud)
问题是,渲染时,&字符将替换为&:
<script src='http://example.com/page.aspx?a=xxx&b=zzz&c=123.45' type='text/javascript'></script>
Run Code Online (Sandbox Code Playgroud)
我很期待这个,显然:
<script src='http://example.com/page.aspx?a=xxx&b=zzz&c=123.45' type='text/javascript'></script>
Run Code Online (Sandbox Code Playgroud)
但是,如果我直接在<script>标签外面渲染网址,它看起来还不错!干得好
<%= string.Format("http://example.com/page.aspx?a={0}&b={1}&c={2:0.00}", A, B, C) %>
Run Code Online (Sandbox Code Playgroud)
呈现这个:
http://example.com/page.aspx?a=xxx&b=zzz&c=123.45
Run Code Online (Sandbox Code Playgroud)
是什么赋予了?我该如何制止这种疯狂?我的强迫症不能服用!
如何让异常处理应用程序块(EHAB)从日志中的Exception.Data属性中写入值?
try
{
// ...
}
catch (Exception ex)
{
ex.Data.Add("Hello", "World");
throw ex;
}
Run Code Online (Sandbox Code Playgroud)
正确记录异常,但我无法在EHAB创建的日志条目中的任何位置找到添加的数据.
据我所知,建议的做法是将其他相关信息添加到异常本身,如上例所示.这就是为什么我有点惊讶EHAB默认不包括这个.
我可以通过使用EntLib文本格式化模板编辑器(下面的屏幕截图)修改模板来解决这个问题吗?我找不到所提供的各种"令牌"的任何信息,但我认为答案隐藏在某处.
文本格式化模板编辑器http://img195.imageshack.us/img195/6614/capturegmg.png
或者我真的需要实现自己的自定义文本格式化程序来实现这一目标吗?
编辑/ UPDATE:
我在我的Global.asax.cs中这样做,以避免在我的代码中的任何地方添加HandleException方法调用:
using EntLib = Microsoft.Practices.EnterpriseLibrary;
using System;
namespace MyApp
{
public class Global : System.Web.HttpApplication
{
protected void Application_Error(object sender, EventArgs e)
{
// I have an "All Exceptions" policy in place...
EntLib.ExceptionHandling.ExceptionPolicy.HandleException(Server.GetLastError(), "All Exceptions");
// I believe it's the GetLastError that's somehow returning a "lessor" exception
}
}
}
Run Code Online (Sandbox Code Playgroud)
事实证明这与此不一样(工作正常,基本上解决了我的问题):
try
{
// ...
} …Run Code Online (Sandbox Code Playgroud) 我正在做一个带有搜索的网页,它会从MSSQL中获取大量信息.我所做的是一个存储过程,只返回要在网站上看到的页面.
现在我正在进行分页,因为我需要显示类似于谷歌的东西.如果您在第1页,他们会显示前10页,如果您在第19页,则显示自第9页到第28页.
我认为显示页码的最佳选择是在转发器中使用链接按钮.我现在遇到的问题是我不知道在回发时获取页码的最佳方法.
做一个快速示例我将一个ArrayList分配给repeater.datasource:
<asp:Repeater ID="Repeater2" runat="server">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandArgument="<%# Container.DataItem %>"><%# Container.DataItem %></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
<asp:LinkButton ID="LinkButton2" runat="server" CommandArgument="4654">Test #1</asp:LinkButton>
Run Code Online (Sandbox Code Playgroud)
在我的Default.aspx.cs文件中,我有下一个代码
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack)
{
string x = LinkButton2.CommandArgument;
//string y = LinkButton1.CommandArgument;
//I know this line will not work since the Linkbutton1 is inside the Repeater.
}
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能使它有效?
有没有人有这个分页更好的解决方案?
谢谢
杰瑞
假设我有一个库,我在其中添加了一些Console.WriteLine(..)语句来帮助我在实现过程中,看看在Console App中使用库时会发生什么.
现在我想在ASP.NET应用程序中使用相同的库.理想情况下,我可以登录到生产网络服务器,并以某种方式启动命令提示符并将其附加到网站,并在发生时实时查看消息.我怎么做?
我正在尝试使用手风琴式功能构建HTML页面.首先,我有这个简单的CSS选择器,声明"如果H1正好在另一个H1之后,则使背景变为红色".
<style type="text/css">
h1 + h1
{
background-color: red;
}
</style>
Run Code Online (Sandbox Code Playgroud)
然后一些纯HTML:
<h1>First</h1>
<p>Blah, blah...</p>
<h1>Second</h1>
<p>Blah, blah...</p>
<h1>Third</h1>
<p>Blah, blah...</p>
Run Code Online (Sandbox Code Playgroud)
所有H1看起来都应该是,没有红色背景,因为它们之前是P标签.
为了这:
<h1>First</h1>
<h1>Second</h1>
<p>Blah, blah...</p>
<h1>Third</h1>
<p>Blah, blah...</p>
Run Code Online (Sandbox Code Playgroud)
第二个H1具有红色背景,因为它与前一个H1之间的P标记被删除.非常好.
现在,我真正想做的是以编程方式隐藏H1下方的"内容",然后我希望看到红色规则开始.像这样:
<h1>First</h1>
<p>Blah, blah...</p>
<h1>Second</h1>
<p style="display: none">Blah, blah...</p>
<h1>Third</h1>
<p>Hello world...</p>
Run Code Online (Sandbox Code Playgroud)
但由于P标记仍然存在,CSS规则不适用于第三个H1,并且没有红色背景.
问题:有没有办法将使用JavaScript的东西应用到P标签,这会导致它们(暂时)被CSS规则忽略?
c# ×6
.net ×5
asp.net ×5
vb.net ×4
caching ×1
css ×1
html ×1
html-encode ×1
iis-7 ×1
linkbutton ×1
locking ×1
logging ×1
paging ×1
razor ×1
repeater ×1
sql-update ×1
t-sql ×1
web-config ×1