【问题标题】:MvcMiniProfiler results request giving 404 in Asp.Net MVC appMvcMiniProfiler 结果请求在 Asp.Net MVC 应用程序中给出 404
【发布时间】:2011-06-27 18:21:08
【问题描述】:

我正在尝试在我的 Asp.Net MVC 应用程序中使用 MvcMiniProfiler。我安装了最新的 NuGet 包,并将 wiki 页面中的所有代码添加到 Application_BeginRequest()Application_AuthenticateRequest() 和我的视图中。

加载页面时,所有迷你分析器的 javascript 文件都被包含并从服务器正确下载,但是当它尝试获取 Chrome 显示的结果时:

GET http://localhost:59269/mini-profiler-results?id=59924d7f-98ba-40fe-9c4a-6a163f7c9b08&popup=1 404 (Not Found)

我认为这是因为没有使用 MVC 设置路由以允许 /mini-profiler-results 但是我找不到这样做的方法。查看 Google 代码页,他们的示例应用程序的Global.asax.cs 文件经历了几次更改,一次使用MvcMiniProfiler.MiniProfiler.RegisterRoutes(),第二次使用MvcMiniProfiler.MiniProfiler.Init(),第三种样式没有任何作用。前面提到的两个功能不存在,所以我假设它们已经被淘汰了。

此时我不确定如何修复此错误并在我的应用程序中使用分析器。有什么想法吗?


我的 Global.Asax.cs 文件如下所示:

public class Global : System.Web.HttpApplication
{
    protected void Application_BeginRequest()
    {
        MvcMiniProfiler.MiniProfiler.Start();
    }

    protected void Application_AuthenticateRequest(Object sender, EventArgs e)
    {
        // Only show profiling to admins
        if (!Roles.IsUserInRole(Constants.AdminRole))
            MvcMiniProfiler.MiniProfiler.Stop(discardResults: true);
    }

    protected void Application_Start(object sender, EventArgs e)
    {
        AreaRegistration.RegisterAllAreas();
        RegisterRoutes(RouteTable.Routes);
    }

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");

        routes.MapRoute(
        "Default",
            // Route name
        "{controller}/{action}/{id}",
            // URL with parameters
        new { controller = "Home", action = "Index", id = "" }
            // Parameter defaults
        );
    }
}

【问题讨论】:

  • 现在看起来这一切都发生在静态构造函数中......看起来......
  • 顺便说一句,为了避免开销,你应该避免调用 Start all;我们基于 cookie 执行此操作,然后我们稍后在管道中进行验证(当用户可用时)。
  • 哦,维基页面让我觉得我上面的做法是通常/公认的做法。我应该改为在 Application_AuthenticateRequest() 方法中调用“Start”吗?
  • 嗯.... 状态再现 ;p 看着
  • 哇哦,很高兴不是我做错了什么疯狂的事情:)

标签: c# asp.net-mvc mvc-mini-profiler


【解决方案1】:

RegisterRoutes() 方法现在由静态构造函数自动调用(并使用适当的写锁等),当您第一次调用 MiniProfiler.Start() 时应该依次调用该静态构造函数。这应该将路由注入到路由表中。

因此,除非您在第一次接触探查器之后 明确清除路由表,否则它应该(所有条件相同)都可以工作。 p>

我想知道这是否是一个安全问题。例如,您运行的是哪个版本的 IIS?在某些配置(尤其是 IIS6)中,路径需要被服务器识别,或者您需要启用通配符。如果是这种情况,请告诉我 - 也许我们可以实现某种到 ashx 或其他东西的后备路由。


更新:问题是您没有在查询结束时保存结果;有短期和长期存储,并且都提供了默认实现 - 您需要做的就是:

protected void Application_EndRequest(object sender, EventArgs e)
{
    MiniProfiler.Stop(discardResults: !IsAnAdmin());
}

更新更新:您可能还需要在 web.config 的 <system.webServer><handlers> 部分添加以下内容:

<add name="MiniProfiler" path="mini-profiler-resources/*" verb="*"
    type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified"
    preCondition="integratedMode" />

【讨论】:

  • 我更新了帖子以显示我的 global.asax.cs 文件,据我所知,我没有清除任何应该影响 MvcMiniProfile 的路由。这是在 IIS Express 上运行的
  • 啊哈就是这样。我错过了入门页面的那部分:)。非常感谢:)
  • +1 - 这一点让我“除非你明确地清除路由表”
  • 在 web.config 中添加 条目让 MiniProfiler 在部署后通常为我工作。本地开发人员根本不需要。虽然 /mini-profiler-resources/results 仍然得到一些 404
  • @DaveD,你找到“404s for /mini-profiler-resources/results”的解决方案了吗?
猜你喜欢
  • 2014-07-27
  • 2019-05-17
  • 2016-08-12
  • 2010-11-09
  • 2021-12-26
  • 2013-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多