/// <summary> /// 执行结果 /// </summary> /// <param name="context">上下文</param> public override void ExecuteResult(RequestContext context) { if (File.Exists(this.FileName) == true) { this.ExecuteFileResult(context); } else { var result = new ErrorResult { Status = 404, Errors = "找不到文件:" + this.FileName }; result.ExecuteResult(context); } }
/// <summary> /// 处理静态资源请求 /// </summary> /// <param name="extension">扩展名</param> /// <param name="requestContext">上下文</param> private void ProcessStaticFileRequest(string extension, RequestContext requestContext) { var contenType = this.MIMECollection.GetContentType(extension); var file = requestContext.Request.Url.AbsolutePath.TrimStart('/').Replace(@"/", @"\"); if (string.IsNullOrWhiteSpace(contenType) == true) { var result = new ErrorResult { Status = 403 }; result.ExecuteResult(requestContext); } else if (File.Exists(file) == false) { var result = new ErrorResult { Status = 404, Errors = "找不到指定的文件 .." }; result.ExecuteResult(requestContext); } else { var result = new FileResult { FileName = file, ContentType = contenType }; result.ExecuteResult(requestContext); } }
/// <summary> /// 异常时 /// </summary> /// <param name="session">产生异常的会话</param> /// <param name="exception">异常</param> protected virtual void OnException(ISession session, Exception exception) { var httpException = exception as HttpException; if (httpException == null) { httpException = new HttpException(500, exception.Message); } var result = new ErrorResult(httpException); var response = new HttpResponse(session); result.ExecuteResult(response); }
/// <summary> /// 处理Api行为执行过程中产生的异常 /// </summary> /// <param name="actionContext">上下文</param> /// <param name="actionfilters">过滤器</param> /// <param name="exception">异常项</param> private void ProcessExecutingException(ActionContext actionContext, IEnumerable<IFilter> actionfilters, Exception exception) { var exceptionContext = new ExceptionContext(actionContext, new ApiExecuteException(exception)); this.ExecAllExceptionFilters(actionfilters, exceptionContext); var result = exceptionContext.Result; if (result == null) { result = new ErrorResult { Status = 500, Errors = exception.Message }; } result.ExecuteResult(actionContext); }