【发布时间】: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
);
}
希望得到大家的好建议。 :)
最近,我还附上了简单的项目文件作为以下链接。
【问题讨论】:
-
您在 IIS 或 Visual Studio Web Server 中遇到此问题?
-
IIS Development Server 内置服务器与VS 2010
-
究竟是什么不起作用?你得到了 404?
-
OggHandler 没有被调用,也没有返回 *.ogg 文件的 application/ogg 内容类型。我没有找到 404 文件。
-
注意路由注册顺序很重要。您之前是否注册了其他路线,甚至重置了路线集合?
标签: asp.net-mvc asp.net-mvc-3 routes