URL 在 ASP.Net 中为非 aspx 文件扩展名路由
URL Routing in ASP.Net for non aspx file extensions
我正尝试在 ASP.Net 中对非 aspx 文件扩展名使用 URL 路由
当我开始使用 asp.net 时,我的代码变得凌乱并且结构化在很多文件夹中
为了隐藏我的目录路径并获得有意义的压缩 URLS 我使用了
URL 路由
有几个文档,对我来说最简单的教程是 http://www.4guysfromrolla.com/articles/051309-1.aspx
默认情况下,URL 路径显示我的完整文件夹结构,为了隐藏此信息,我使用 URL 路由
在以下代码之后,我被允许使用虚拟路径重定向
RouteTable.Routes.Add("login", new Route("login", new RouteHandler(string.Format("~/…/Login.aspx"))));
如果您使用像 HTML 这样的非 .aspx 文件扩展名,您需要在 web.config 中为该扩展名添加 Buildproviders
示例:
RouteTable.Routes.Add("testhtml", new Route("testhtml", new RouteHandler(string.Format("~/.../test.html"))));
Web.Config:
<system.web>
<compilation debug="true" targetFramework="4.6.1" >
<buildProviders >
<add extension=".html" type="System.Web.Compilation.PageBuildProvider"/>
</buildProviders>
</compilation>
<…>
现在 http://localhost:58119/testhtml is the same as http://localhost:58119/.../test.html 具有完整路径
我的问题
默认情况下 ASP.net 可以重定向到 ~/.../test.pdf 或 ~/.../test.png.
与 URL 再次路由它要求文件扩展名的 buildproviders。
但是如果我是正确的话,msdn 文档中没有这些扩展的默认构建提供程序:/
我自己想出来的
Web.config 中的构建提供程序是允许文件访问所必需的,但如果您路由到文件,它们默认情况下不会自动填充 HTTP header 内容类型。
为了手动设置它,我使用了以下代码:
RouteTable.Routes.Add("testpng", new Route("testpng", new CustomRouteHandler(string.Format("~/test.png"))));
.
public class CustomRouteHandler: IRouteHandler
{
string _virtualPath;
public CustomRouteHandler(string virtualPath)
{
_virtualPath = virtualPath;
}
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
requestContext.HttpContext.Response.Clear();
requestContext.HttpContext.Response.ContentType = GetContentType(_virtualPath);
string filepath = requestContext.HttpContext.Server.MapPath(_virtualPath);
requestContext.HttpContext.Response.WriteFile(filepath);
requestContext.HttpContext.Response.End();
return null;
}
private static string GetContentType(String path)
{
switch (System.IO.Path.GetExtension(path))
{
case ".pdf": return "application/pdf";
case ".bmp": return "Image/bmp";
case ".gif": return "Image/gif";
case ".jpg": return "Image/jpeg";
case ".png": return "Image/png";
default: return "text/html";
}
//return "";
}
}
所有允许的 ContenType 列表
http://www.iana.org/assignments/media-types/media-types.xhtml
Web.Config 示例:
<system.web>
<compilation debug="true" targetFramework="4.6.1" >
<buildProviders >
<add extension=".CMDOC" type="System.Web.Compilation.PageBuildProvider"/>
<add extension=".png" type="System.Web.Compilation.PageBuildProvider"/>
<add extension=".pdf" type="System.Web.Compilation.PageBuildProvider"/>
</buildProviders>
</compilation>
我正尝试在 ASP.Net 中对非 aspx 文件扩展名使用 URL 路由
当我开始使用 asp.net 时,我的代码变得凌乱并且结构化在很多文件夹中 为了隐藏我的目录路径并获得有意义的压缩 URLS 我使用了 URL 路由 有几个文档,对我来说最简单的教程是 http://www.4guysfromrolla.com/articles/051309-1.aspx
默认情况下,URL 路径显示我的完整文件夹结构,为了隐藏此信息,我使用 URL 路由 在以下代码之后,我被允许使用虚拟路径重定向
RouteTable.Routes.Add("login", new Route("login", new RouteHandler(string.Format("~/…/Login.aspx"))));
如果您使用像 HTML 这样的非 .aspx 文件扩展名,您需要在 web.config 中为该扩展名添加 Buildproviders
示例:
RouteTable.Routes.Add("testhtml", new Route("testhtml", new RouteHandler(string.Format("~/.../test.html"))));
Web.Config:
<system.web>
<compilation debug="true" targetFramework="4.6.1" >
<buildProviders >
<add extension=".html" type="System.Web.Compilation.PageBuildProvider"/>
</buildProviders>
</compilation>
<…>
现在 http://localhost:58119/testhtml is the same as http://localhost:58119/.../test.html 具有完整路径
我的问题
默认情况下 ASP.net 可以重定向到 ~/.../test.pdf 或 ~/.../test.png.
与 URL 再次路由它要求文件扩展名的 buildproviders。
但是如果我是正确的话,msdn 文档中没有这些扩展的默认构建提供程序:/
我自己想出来的
Web.config 中的构建提供程序是允许文件访问所必需的,但如果您路由到文件,它们默认情况下不会自动填充 HTTP header 内容类型。 为了手动设置它,我使用了以下代码:
RouteTable.Routes.Add("testpng", new Route("testpng", new CustomRouteHandler(string.Format("~/test.png"))));
.
public class CustomRouteHandler: IRouteHandler
{
string _virtualPath;
public CustomRouteHandler(string virtualPath)
{
_virtualPath = virtualPath;
}
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
requestContext.HttpContext.Response.Clear();
requestContext.HttpContext.Response.ContentType = GetContentType(_virtualPath);
string filepath = requestContext.HttpContext.Server.MapPath(_virtualPath);
requestContext.HttpContext.Response.WriteFile(filepath);
requestContext.HttpContext.Response.End();
return null;
}
private static string GetContentType(String path)
{
switch (System.IO.Path.GetExtension(path))
{
case ".pdf": return "application/pdf";
case ".bmp": return "Image/bmp";
case ".gif": return "Image/gif";
case ".jpg": return "Image/jpeg";
case ".png": return "Image/png";
default: return "text/html";
}
//return "";
}
}
所有允许的 ContenType 列表 http://www.iana.org/assignments/media-types/media-types.xhtml
Web.Config 示例:
<system.web>
<compilation debug="true" targetFramework="4.6.1" >
<buildProviders >
<add extension=".CMDOC" type="System.Web.Compilation.PageBuildProvider"/>
<add extension=".png" type="System.Web.Compilation.PageBuildProvider"/>
<add extension=".pdf" type="System.Web.Compilation.PageBuildProvider"/>
</buildProviders>
</compilation>