【问题标题】:URL Routing in ASP.Net for non aspx file extensionsASP.Net 中用于非 aspx 文件扩展名的 URL 路由
【发布时间】:2019-02-19 16:10:25
【问题描述】:

我正在尝试将 ASP.Net 中的 URL 路由用于非 aspx 文件扩展名

当我开始使用 asp.net 时,我的代码变得杂乱无章,并且结构在很多文件夹中 为了隐藏我的目录路径并获得我使用的有意义的综合 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/testhtmlhttp://localhost:58119/.../test.html 相同,路径完整

我的问题

默认 ASP.net 可以重定向到 ~/…/test.pdf 或 ~/…/test.png。

使用 URL 路由,它再次请求构建提供程序的文件扩展名。

但是如果我是正确的,msdn 文档中没有这些扩展的默认构建提供程序:/

【问题讨论】:

    标签: c# asp.net url-routing file-extension buildprovider


    【解决方案1】:

    我自己想出来的。

    Web.config 中的 Build Providers 是允许文件访问所必需的,但如果您使用文件,默认情况下它们不会自动填充 HTTP 标头内容类型。 手动设置它我使用了以下代码:

            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 "";
        }
    }
    

    所有允许的内容类型列表 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>
    

    【讨论】:

      猜你喜欢
      • 2019-06-26
      • 2013-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-16
      • 2011-10-01
      • 2011-04-02
      • 1970-01-01
      相关资源
      最近更新 更多