0

I'm trying to Use URL Routing in ASP.Net for non aspx file extensions

As i started playing around with asp.net my code got messy and was structured in alot of folders To hide my Directory paths and get meaningful comprimated URLS i used URL Routing There are several Documantation on that, for me the easyest Tutorial was https://web.archive.org/web/20201205221404/https://www.4guysfromrolla.com/articles/051309-1.aspx

On default the URL paths show my complete folder structure, to hide this information i use URL Routing After the following code, i was allowed to use redirect with the virtual paths

    RouteTable.Routes.Add("login", new Route("login", new RouteHandler(string.Format("~/…/Login.aspx"))));

If you use non .aspx file extensions like HTML you need to add Buildproviders in web.config for that extension

Example:

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>
<…>

Now http://localhost:58119/testhtml is the same as http://localhost:58119/.../test.html with complete path

To my Question

On default ASP.net can ridirect to ~/…/test.pdf or ~/…/test.png.

with URL Routing it again asks for buildproviders for the file extension.

But there are no default buildproviders for these extensions in the msdn documentation if i'm correct :/

Paul Pascher
  • 317
  • 2
  • 14

1 Answers1

1

I figuerd it out myself.

the Build Providers in Web.config are necessary to allow file acess but they dont auto fill the HTTP header Conten Type by default if you rute to files. to set it by hand i used the following code:

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

List of all allowed ContenTypes http://www.iana.org/assignments/media-types/media-types.xhtml

Web.Config example:

  <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>
Paul Pascher
  • 317
  • 2
  • 14