简体   繁体   中英

Map route in webforms with url extension

I'm working on an old application where I need a webForm to read query strings and return an asset (an image in most cases). My route looks like this:

routes.MapPageRoute("Assets", "Assets/{moduleId}/{fileName}", "~/Assets.aspx");

My code to return an asset:

 protected void Page_Load(object sender, EventArgs e)
    {
        string moduleId = RouteData.Values["moduleId"] as string;
        string fileName = RouteData.Values["filename"] as string;
        Repo repo = new Repo();
        var fileBytes = repo.GetFileBytes(moduleId, fileName);
        var mimeType = repo.GetMimeType(moduleId, fileName);


            Response.ContentType = mimeType;
            Response.BinaryWrite(fileBytes);
        }

If I hardcode the filename in the method, it works ok. My problem is that whenever I add the extension to the filename, I get a 404. Example:

If I open in the browser localhost/Assets/1/2 then moduleId is 1 and fileName is 2

If I open localhost/Assets/1/2.jpg I get a 404

How can I hit Assets.aspx when my filename has an extension?

I know that a url like Assets.aspx?moduleId=1&fileName=2.jpg would work, but I need it to in the form of Assets/1/2.jpg

Solved it by adding the following to my web.config:

<add name="Assets" path="Assets/**/*" verb="GET" type="AssetsHandler" resourceType="Unspecified" preCondition="integratedMode" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM