如果文件不存在或用户无权下载该特定文件,我想返回404错误.
可能吗?如果是,怎么样?示例代码将不胜感激.
解决方法 检查此代码public class MyHandler : IhttpHandler{ private const string CONSTSOMEParaM = "SomeParam"; public MyHandler() { } public voID ProcessRequest(httpContext context) { // Don't allow this response to be cached by the browser. // Note,you MIGHT want to allow it to be cached,depending on what you're doing. context.Response.Cache.SetCacheability(httpCacheability.NoCache); context.Response.Cache.SetNoStore(); context.Response.Cache.SetExpires(DateTime.MinValue); if (ValIDateParameters(context) == false) { //Internal Server Error context.Response.StatusCode = 500; context.Response.End(); return; } if (context.User.IDentity.IsAuthenticated == false) { //ForbIDden context.Response.StatusCode = 403; context.Response.End(); return; } string someParam = context.Request.queryString[CONSTSOMEParaM]; SomethingReponse response = SomeService.someimportantThing(someParam); if (response.header == null || response.header.Success == false) { //Whatever wasn't found context.Response.StatusCode = 404; context.Response.End(); return; } context.Response.ContentType = "somespecific/mimetype"; context.Response.BinaryWrite(); //or context.Response.Write(); //or context.Response.Writefile(); //or someimagestream.Save(context.Response.OutputStream); } public bool ValIDateParameters(httpContext context) { //ValIDate some stuff...true if cool,false if not return false; } /// <summary> /// True if this handler can be reused between calls. That's cool if you don't have any class instance data. /// False if you'd rather get a fresh one. /// </summary> public bool IsReusable { get { return true; } }}总结
以上是内存溢出为你收集整理的c# – 如何从asp.net处理程序返回404错误?全部内容,希望文章能够帮你解决c# – 如何从asp.net处理程序返回404错误?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)