我已按照MiniProfiler文档中的所有步骤操作,并能够查看特定呼叫的结果。
但是当尝试将其添加到Global.asax页面时,它会抛出一个错误,如:
异常详细信息:System.Web.HttpException:请求在此上下文中不可用
MiniProfiler也可以在ASP.NET网站上运行,还是仅适用于Web应用程序?
我正在通过NuGet MiniProfiler添加软件包。
Global.asax代码页
<%@ Application Language="C#" %>
<%@ Import Namespace="StackExchange.Profiling" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
MiniProfiler.Start();
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
MiniProfiler.Stop();
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
}
</script>
答案 0 :(得分:2)
我有点不对,上次我用的是ASP.MVC。但我已经设法让你尝试一个有效的例子:
<强> Global.asax中强>
private bool isStarted;
protected void Application_Start(object sender, EventArgs e)
{
this.isStarted = false;
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (!isStarted)
{
this.isStarted = true;
if (Request.IsLocal)
{
MiniProfiler.Start();
}
}
}
protected void Application_EndRequest(object sender, EventArgs e)
{
MiniProfiler.Stop();
}
<强> Page.aspx 强>
protected void Page_Load(object sender, EventArgs e)
{
var miniprofiler = MiniProfiler.Current;
var htmlString = miniprofiler.Render();
Literal1.Text = htmlString.ToString();
}
这需要一些调整,但至少它会给你一些工作。我并不完全相信mini-profiler可以用于ASP.Net网络表单。
哦,Literal1
只是标准的Literal控件。