如何将 ActivityId (GUID) 传递给 aspx 页面

How to pass ActivityId (GUID) to aspx page

在 Global.asax 中,我添加了这个方法,这样我就可以为每个请求获取 GUID

protected void Application_BeginRequest(object sender, EventArgs e)
{
    Trace.CorrelationManager.ActivityId = Guid.NewGuid();
}

我可以将此 GUID 写入我的 NLog 文件。

我如何将它传递到我的自定义 aspx 页面(错误页面)以便用户可以发送我的这个 GUID 以便我轻松查看错误文件?

在 asax.cs 文件中,我试图在 Page_Load 中添加这个

string _SessionGuid = Trace.CorrelationManager.ActivityId.ToString();

但是 VS 说这个错误,虽然我有 System.Diagnostics 参考:

Error 21 'System.Web.TraceContext' does not contain a definition for 'CorrelationManager' and no extension method 'CorrelationManager' accepting a first argument of type 'System.Web.TraceContext' could be found (are you missing a using directive or an assembly reference?) C:...\ErrorPage.aspx.cs

我自己解决了。

在 HomeController 中我有

public class HomeController : Controller
{
    private readonly string _SessionGuid = 
        Trace.CorrelationManager.ActivityId.ToString();
....
}

并且在 ActionResult 中这个

public ActionResult About()
{
   Session["test"] = _SessionGuid;
   ....
}

当错误发生时,我将它路由到 ErrorController,其中包含这段代码

public class ErrorController : Controller
{
    public ActionResult Index()
    {
        var errGUID = (string)Session["test"];
        ViewBag.ErrorMessage = errGUID;

        return View();
    }
}