【问题标题】:Custom Route is not called for ASP.NET MVC 3 application .NET 4没有为 ASP.NET MVC 3 应用程序 .NET 4 调用自定义路由
【发布时间】:2011-12-18 09:46:02
【问题描述】:

我尝试为 *.ogg 文件创建自定义路由,该文件在带有 HTML 5 音频标签的网站中播放。

事实上,如果我们不提供 application/ogg 作为 ContentType,FireFox 就不会播放 ogg。 不像 Chrome 播放 ogg 不需要额外的工作。

顺便说一句,我认为自定义路由并将其注册到 Global.asax 是一个很好的解决方案和可重用的代码。 对于每个带有 .ogg 扩展名的路径,服务器都会发送带有 application/ogg ContentType 的 ogg 文件。

问题是我不知道为什么带有 *.ogg 的路径没有被自定义路由处理。

这是我的 OggHandler.cs 代码

 public class OggHandler : IRouteHandler, IHttpHandler
{

    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        return this;
    }


    /// <summary>
    /// process ogg file
    /// </summary>
    /// <param name="context"></param>
    public void ProcessRequest(HttpContext context)
    {
        var request = context.Request;
        var response = context.Response;


        try
        {
            //response ogg file with Content Type Header
            response.ContentType = "application/ogg";
            response.WriteFile(request.PhysicalPath);
        }
        catch (Exception ex)
        {
            response.Write("<html>\r\n");
            response.Write("<head><title>Ogg HTTP Handler</title></head>\r\n");
            response.Write("<body>\r\n");
            response.Write("<h1>" + ex.Message + "</h1>\r\n");
            response.Write("</body>\r\n");
            response.Write("</html>");
        }
    }

    public bool IsReusable
    {
        get { return true; }
    }




}

以及在 Global.asax 中的注册

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

        routes.Add(new Route
                       (
                       "{resource}.ogg/{*pathInfo}"
                       , new OggHandler()
                       ));



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

    }

希望得到大家的好建议。 :)

最近,我还附上了简单的项目文件作为以下链接。

http://codesanook.com/shared/MvcOgg.zip

【问题讨论】:

  • 您在 IIS 或 Visual Studio Web Server 中遇到此问题?
  • IIS Development Server 内置服务器与VS 2010
  • 究竟是什么不起作用?你得到了 404?
  • OggHandler 没有被调用,也没有返回 *.ogg 文件的 application/ogg 内容类型。我没有找到 404 文件。
  • 注意路由注册顺序很重要。您之前是否注册了其他路线,甚至重置了路线集合?

标签: asp.net-mvc asp.net-mvc-3 routes


【解决方案1】:

首先要做的是:如果您在 IIS 或 web.config Web 服务器部分中指定映射到 .ogg 文件的其他应用程序/ogg mimetype,您是否检查过它是否不起作用?

然后,我认为问题在于您的路由注册将仅匹配单段 URL (/filename.ogg) 。您应该将路由模式指定为 {*musicpath}/.ogg

第三件事,比其他的更具风格,你应该创建一个 oggroutehandler 类来创建 ogghttphandler。这样你就可以保持两种能力的分离。

抱歉,我无法更好地检查它,但我使用的是智能手机 :) 高温

西蒙娜

【讨论】:

  • 嗨 Simone,非常感谢您提供有用的建议。因为我没有配置 IIS 的授权,我可以做的是在 app.xml 中编码。然而,我还没有完成。如果你能检查我附加的项目codesanook.com/shared/MvcOgg.zip 并帮助我解决它,我会很高兴。 :)
猜你喜欢
  • 1970-01-01
  • 2015-06-22
  • 1970-01-01
  • 1970-01-01
  • 2013-11-05
  • 1970-01-01
  • 2020-04-17
  • 1970-01-01
相关资源
最近更新 更多