- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个自定义模型绑定(bind)器,它从 MEF 容器中提取接口(interface)的实现。它的实现如下:
public class PetViewModelBinder : DefaultModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var petId = bindingContext.ValueProvider.GetValue("id");
var container = controllerContext.HttpContext.Application[MvcApplication.PLUGINS] as CompositionContainer;
Lazy<IPet, IPetMetadata> pet = null;
try
{
pet = container.GetExport(typeof(IPet), petId);
var petVM = new Models.PetViewModel(pet);
bindingContext.ModelMetadata.Model = petVM;
return base.BindModel(controllerContext, bindingContext);
}
catch (Exception)
{
throw;
}
finally
{
container.ReleaseExport(pet);
}
}
当 MEF 具有 petId 的导出时,这非常有效...但是当导出不存在时返回 http 状态 500(服务器错误)。错误消息混淆要求规定应返回 http 状态 403(禁止)。
如何捕获错误、更改响应状态、不返回内容或重新路由 Action 来处理这种情况?
最佳答案
如果你想返回一个特定的 http 状态代码,你应该从 Controller 或操作过滤器中返回。
执行此操作的一种方法是从您的模型绑定(bind)器返回 null 并在您的 Controller 中处理它。但是,这有点粗糙,因此您将无法区分不同的错误。
另一种方法是抛出一个特定的异常并在您的(全局)错误处理中处理它。自定义的 HandleError Action 过滤器可以做到这一点:
public class CustomHandleErrorAttribute : HandleErrorAttribute
{
public int StatusCode { get; set; }
public override void OnException( ExceptionContext filterContext )
{
base.OnException( filterContext );
if ( StatusCode > 0 )
{
filterContext.HttpContext.Response.StatusCode = StatusCode;
}
}
}
在你的 Controller 中,用这个属性装饰 Action :
[CustomHandleError( ExceptionType = typeof (NotAllowedException), View = "~/Views/Shared/Error.cshtml",
StatusCode = 403 )]
public ActionResult Index( FancyModel model )
{
return View( model );
}
最后,在您的模型绑定(bind)器中抛出 NotAllowedException,这是您还需要定义的自定义异常类型。
请注意,如果您在 web.config 文件中启用了自定义错误,这仅适用于您的开发设置。
关于asp.net-mvc-3 - 如何从 Custom Model Binder 返回 HTTP 状态代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11184802/
我有一个自定义模型绑定(bind)器,它会为进入操作方法的特定参数调用: public override ActionResult MyAction(int someData, [ModelBinde
我正在尝试从服务下载图像并将其显示在 Activity 中,但我不断收到 java binder FAILED BINDER TRANSACTION 这是我的服务代码 public class Do
我有下一个问题,我正在创建自定义适配器的 ListView ,我正在使用异步任务从 JSON url(文本和图像 url)下载适配器的数据,然后我将适配器设置为列表,我使用毕加索库下载图像,现在我的应
我正在尝试将一个 UI 活页夹小部件包含到另一个小部件中:我正在使用 UI 活页夹构建一个表单,并希望包含一个也使用 UI 活页夹完成的精美按钮。有谁知道如何做到这一点? 谢谢! 最佳答案 是的,这是
我收到这个错误: Attempt to invoke interface method 'android.os.IBinder com.mediatek.anrappmanager.IFramewor
我想问一下有什么区别 bindConstant().annotatedWith(Names.named("keepAliveInterval")).to(60); 和 bind(Integer.TYP
我的应用程序已编译,然后在我的手机中安装 apk 时弹出窗口询问我新安装将卸载以前的版本,然后安装新版本。我点击了确定,但过了一会儿它显示了这个错误:- 01/14 10:43:22: Launchi
谁能告诉我这是什么意思? 一切都很好,我没有改变任何东西,只是发生了,这是 Binder 565 中的代码: try { res = onTransact(code, data, reply,
我的项目 application.properties 发出警告。说 'spring.cloud.stream.kafka.binder.jaas.loginModule' is an unknown
我读了一篇关于 android 的 Binder 的文章。文章说进程交换共享内存中的对象引用并且它比编码和解码更有效......但实际上IPC机制中是否存在编码和解码?我有点困惑... 谁能解释绑定(
在与 Oleg 讨论期间对问题进行了大量编辑 我正在尝试在 Spring Cloud Stream 中实现 BigQuery 的绑定(bind)器。 完整的应用程序代码可在GitHub上获取. 到目前
我有一个 Android Java 服务,它通过 Binder (AIDL) 与 native 进程交互。 我看到该服务列在“adb shell 服务列表”下。我也可以使用“adb shell ser
我创建了一个在单独进程中运行的服务 我读过,当服务作为单独的进程进行时,它是在一个单独的线程上而不是在主线程上。当我在服务中执行 HTTP 请求时,出现异常:Thread forbids Http
Android ContentProvider 使用“绑定(bind)线程”池来处理 RPC,例如 query、insert 和 call。在我的 ContentProvider 实现中,我正在执行一
我正在尝试阅读Android源代码来了解binder,但我无法找到Android系统架构中描述的binder驱动程序部分。知道 Binder 代码位于 Android 源代码中的什么位置吗? 最佳答案
08-27 22:17:20.980: INFO/AndroidRuntime(299): 注意:线程 'Binder Thread #3' 的附加失败这是 LogCat 信息,请帮助我。 最佳答案
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我已经用我的界面定义了一个 AIDL 文件。像这样: interface IResPlugin { int discoverType(); Map onClick( in int id
我正在做一个项目,我们向 android 系统添加了一些非标准的安全功能,但我在调整 Binder 时遇到了一些严重的问题。 有没有人对Binder系统有很深的了解,知道Binder为什么会“阻塞”一
我想我追踪到了内存泄漏,并想确认我认为 Android 的 Binder 是如何实现的。在这种情况下,我有一个服务和一个 Activity ,每个都在自己的进程中。我创建了一个 AIDL,它允许我通过
我是一名优秀的程序员,十分优秀!