如何使用热毛巾mvc4一瞥?

时间:2013-06-07 06:28:06

标签: asp.net-mvc-4 hottowel glimpse

我想在我的mvc4热巾项目中添加一瞥诊断工具。

我正在遵循指令http://nuget.org/packages/Glimpse.Mvc4/并在我的项目中安装mvc4 nuget package of glimpse。

正在正确安装并获得依赖关系并更新web.config,如下所示。

<httpModules>
  <add name="Glimpse" type="Glimpse.AspNet.HttpModule, Glimpse.AspNet" />
</httpModules>
<httpHandlers>
  <add path="glimpse.axd" verb="GET" type="Glimpse.AspNet.HttpHandler, Glimpse.AspNet" />
</httpHandlers>

<handlers>
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
  <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
  <add name="Glimpse" path="glimpse.axd" verb="GET" type="Glimpse.AspNet.HttpHandler, Glimpse.AspNet" preCondition="integratedMode" />
</handlers>
<validation validateIntegratedModeConfiguration="false" />
<modules>
  <add name="Glimpse" type="Glimpse.AspNet.HttpModule, Glimpse.AspNet" preCondition="integratedMode" />
</modules>

<glimpse defaultRuntimePolicy="On" endpointBaseUri="~/Glimpse.axd">
<!-- If you are having issues with Glimpse, please include this. It will help us figure out whats going on. 
  <logging level="Trace" />-->
<!-- Want to use Glimpse on a remote server? Ignore the LocalPolicy by removing this comment.
  <runtimePolicies>
    <ignoredTypes>
      <add type="Glimpse.AspNet.Policy.LocalPolicy, Glimpse.AspNet"/>
    </ignoredTypes>
  </runtimePolicies>-->

但是,当我运行项目并输入http://mylocalurl.com/glimpse.axd等网址时。它没有找到axd文件所以我无法启用一瞥。

P.S。我已经定义了自定义路由来加载热毛巾模板的索引页面。

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

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Load", action = "Index", id = UrlParameter.Optional }
        );
    }

2 个答案:

答案 0 :(得分:3)

无法找到Glimpse.axd的原因是由于路由的配置顺序。

如果您安装了RouteDebugger NuGet package,您将看到有另一条路由处理Glimpse.axd请求,即Hot Towel模板安装的路由,可以在App_Start文件夹中找到在班级HotTowelRouteConfig

using System.Web.Mvc;

[assembly: WebActivator.PreApplicationStartMethod(
    typeof(throwaway.App_Start.HotTowelRouteConfig), "RegisterHotTowelPreStart", Order = 2)]

namespace throwaway.App_Start {
  ///<summary>
  /// Inserts the HotTowel SPA sample view controller to the front of all MVC routes
  /// so that the HotTowel SPA sample becomes the default page.
  ///</summary>
  ///<remarks>
  /// This class is discovered and run during startup
  /// http://blogs.msdn.com/b/davidebb/archive/2010/10/11/light-up-your-nupacks-with-startup-code-and-webactivator.aspx
  ///</remarks>
  public static class HotTowelRouteConfig {

    public static void RegisterHotTowelPreStart() {

      // Preempt standard default MVC page routing to go to HotTowel Sample
      System.Web.Routing.RouteTable.Routes.MapRoute(
          name: "HotTowelMvc",
          url: "{controller}/{action}/{id}",
          defaults: new
          {
              controller = "HotTowel",
              action = "Index",
              id = UrlParameter.Optional
          }
      );
    }
  }
}

由于此路由将在RouteConfig.RegisterRoutes执行之前添加(由于使用了WebActivator),因此它将处理Glimpse.axd的请求,因为该路由具有每个路由参数的默认值,因此将匹配Glimpse.axd请求。

您可以通过移除HotTowelRouteConfig来解决此问题,但是您必须明确地将示例网址http://mylocalurl.com/HotTowel/Index移除,或者从WebActivator移除HotTowelRouteConfig部分

[assembly: WebActivator.PreApplicationStartMethod(
    typeof(throwaway.App_Start.HotTowelRouteConfig), "RegisterHotTowelPreStart", Order = 2)]

并在调用RouteConfig.RegisterRoutes之后通过调用HotTowelRouteConfig.RegisterHotTowelPreStart()routes.IgnoreRoute("{resource}.axd/{*pathInfo}");中明确注册路线,这样您默认情况下仍会拥有该示例,而Glimpse.axd将被处理为预期

无论哪种方式,这两个选项都涉及删除或更改由HotTowel NuGet包管理的文件,因此可能将该示例放入其自己的NuGet包中(例如,您不希望将该示例投入生产)

答案 1 :(得分:1)

签入Hot Towel GitHub repository的最新源代码处理略有不同。

HotTowelRouteConfig中,RegisterHotTowlPreStart功能更改为:

public static void RegisterHotTowelPreStart() {

  // Ignore requests to .axd HttpHandlers
  System.Web.Routing.RouteTable.Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

  // Preempt standard default MVC page routing to go to HotTowel Sample
  System.Web.Routing.RouteTable.Routes.MapRoute(
      name: "HotTowelMvc",
      url: "{controller}/{action}/{id}",
      defaults: new
      {
          controller = "HotTowel",
          action = "Index",
          id = UrlParameter.Optional
      }
  );

这允许包括axd在内的Glimpse.axd工作,方法是将其排除在样本处理之外。