小编c0b*_*bra的帖子

如何从WebApi消息处理程序中优雅地返回异常

我在我的Mvc\WebApi应用程序中有一个全局ExceptionFilter,注册为:

public virtual void RegisterHttpFilters(HttpConfiguration config)
{
    config.Filters.Add(new MyExceptionFilter(_exceptionHandler));
}
Run Code Online (Sandbox Code Playgroud)

MyExceptionFilter的位置是:

public class MyExceptionFilter : ExceptionFilterAttribute
{
    private readonly IMyExceptionHandler m_exceptionHandler;

    public MyExceptionFilter(IMyExceptionHandler exceptionHandler)
    {
        m_exceptionHandler = exceptionHandler;
    }

    public override void OnException(HttpActionExecutedContext context)
    {
        Exception ex = context.Exception;
        if (ex != null)
        {
            object response = null;
            HttpStatusCode statusCode = m_exceptionHandler != null
                ? m_exceptionHandler.HandleException(ex, out response)
                : HttpStatusCode.InternalServerError;
            context.Response = context.Request.CreateResponse(statusCode, response ?? ex);
        }

        base.OnException(context);
    }
}
Run Code Online (Sandbox Code Playgroud)

此过滤器将所有异常作为json-objects返回,并允许某些IMyExceptionHandler实现自定义要返回的对象.

这一切都运作良好.直到我的一些消息处理程序中有一个例外:

public class FooMessageHandler : DelegatingHandler
{
    private readonly …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-web-api

13
推荐指数
3
解决办法
2万
查看次数

angularjs $ resource类级回调或后处理

我有一个$resourceAPI将始终返回一些需要在进入表示层之前清理的数据.具体来说,它是.NET以可爱的'/Date(...)/'格式返回Date对象.

我不想每次打电话.query()或写电话都要写回电.get().是否有某种方法可以使用一个回调来扩展资源,该回调可以在更新实例属性的REST方法上调用,或者$watch在date属性更改时添加一些被触发的类型?基本上每件事都会发生$resource.

angular.module('myAppServices', ['ngResource'])
    .factory('Participant', ['$resource', function ($resource) {
        var res = $resource('api/url/participants/:id', { id: '@id' });

        // This obviously doesn't work, but something kinda like this?
        res.prototype.$watch(this.FieldName, function(newVal, oldVal) {
            if (needsCleaning(newVal.fieldName) {
                this.FieldName = cleanupField(newVal);
            }
        };
    });
Run Code Online (Sandbox Code Playgroud)

javascript angularjs ngresource

9
推荐指数
2
解决办法
3464
查看次数

使用angular-ui-grid过滤空值的最佳方法是什么?

我想为我的用户提供仅显示特定列的空值的行的功能.什么是最好的方法?我想出了这个解决方案,对我来说很麻烦,重用过滤器输入文本字段:

"onRegisterApi" : function(gridApi){
  $scope.gridApi = gridApi;
  $scope.gridApi.core.on.filterChanged( $scope, function() {
    var grid = this.grid;
    var term = grid.columns[7].filters[0].term; // the column I am interested in empty values
    if( term == '_' ) {
      $log.info("Setting filter to external mode");
      $scope.grid.useExternalFiltering = true;
      for(var i=0; i< grid.rows.length;i++){
        var row = grid.rows[i];
        if(row.entity.privileges==null || row.entity.privileges.trim().length==0){
          $scope.gridApi.core.clearRowInvisible(row);
        }
        else{
          $scope.gridApi.core.setRowInvisible(row);
        }
      }
    }else if($scope.grid.useExternalFiltering==true){
      $log.info("Resetting filter back to internal mode");
      $scope.grid.useExternalFiltering = false;
      for(var i=0; i< grid.rows.length;i++){
        $scope.gridApi.core.clearRowInvisible(grid.rows[i]);
      }
    }
  });
}
Run Code Online (Sandbox Code Playgroud)

angular-ui-grid

5
推荐指数
0
解决办法
1263
查看次数