好的,这可以完成,但您需要使用HttpHandler
. 你可以在这里找到一个很好的例子,但我会详细说明重要的部分。我不能在这里为您编写整个处理程序。
首先,让我们在 web 项目中构建一个类并调用它ImageHandler
...
public class ImageHandler : IHttpHandler
{
}
...接下来让我们实现接口...
public bool IsReusable
{
get { return false; }
}
public void ProcessRequest(HttpContext context)
{
// find out what we're trying to do first
string method = context.Request.HttpMethod;
switch (method)
{
case "GET":
// read the query string for the document name or ID
// read the file in from the shared folder
// write those bytes to the response, ensuring to set the Reponse.ContentType
// and also remember to issue Reponse.Clear()
break;
case "PUT":
// read the Headers from the Request to get the byte[] of the file to CREATE
// write those bytes to disk
// construct a 200 response
break;
case "POST":
// read the Headers from the Request to get the byte[] of the file to UPDATE
// write those bytes to disk
// construct a 200 response
break;
case "DELETE":
// read the Headers from the Request to get the byte[] of the file to DELETE
// write those bytes to disk
// construct a 200 response
break;
}
}
...最后我们需要在web.config
...中设置处理程序
<configuration>
<system.web>
<httpHandlers>
<!-- remember that you need to replace the {YourNamespace} with your fully qualified -->
<!-- namespace and you need to replace {YourAssemblyName} with your assembly name -->
<!-- EXCLUDING the .dll -->
<add verb="*" path="*/images/*" type="{YourNamespace}.ImageHandler, {YourAssemblyName}" />
</httpHandlers>
</system.web>
</configuration>
最后,您还要做的事情是传递某种会话密钥,当您进入处理程序时可以验证该会话密钥,否则这对所有人都是开放的。如果你不需要PUT
,POST
和DELETE
动词也没关系,但你需要。
GET
从技术上讲,如果您不关心每个人都可以访问,则不需要检查会话密钥GET
,但您必须检查其他人。