How do I get the MIME type of a file being requested in ASP.NET C#?

前端 未结 5 2048
醉话见心
醉话见心 2020-12-15 01:06

I would like to handle requests differently depending upon the MIME type. For example, I have PDF\'s, images and other media files that I would like to prohibit access to ba

5条回答
  •  不思量自难忘°
    2020-12-15 01:53

    If I am understanding your question properly, you are serving static files and want to be able to do processing on a static file request in order to decide whether or not the user has access to that file. (based on MIME type)

    If you map all files requests through a custom IHttpHandler (see the handlers section of your web.config file), you should be able to accomplish this.

    In ProcessRequest (or BeginProcessRequest if you implement an asynchronous handler), you can call HttpContext.Current.Server.MapPath("~" + HttpContext.Current.Request.Path) (might be a better way to do that) to get the current static file being requested.

    You can then analyze the extension of that file to make your decision.

    Not sure if thats what you want, but hopefully it helps

提交回复
热议问题