C# 传递函数<&燃气轮机;我的MVC4中的jsonResponseObj类

C# 传递函数<&燃气轮机;我的MVC4中的jsonResponseObj类,c#,asp.net-mvc-4,json.net,func,C#,Asp.net Mvc 4,Json.net,Func,我在MVC4站点中使用了大量的Json.NET,我试图通过在JsonNetResult类中使用FUNC来简化一些事情。我发现,当我编写一个JsonNetResult控制器操作时,我将所有内容包装在同一个try/catch语句中,因此我在JsonNetResult类中创建了一个方法,该方法将Func作为输入。但是,我发现作为输入的对象不起作用-我必须具体一点,并且我只创建了一系列重载方法,这些重载方法在Func中使用不同的类型作为参数 在这种情况下,有没有办法使用对象作为输入?有没有更好的方法来做

我在MVC4站点中使用了大量的Json.NET,我试图通过在JsonNetResult类中使用FUNC来简化一些事情。我发现,当我编写一个JsonNetResult控制器操作时,我将所有内容包装在同一个try/catch语句中,因此我在JsonNetResult类中创建了一个方法,该方法将Func作为输入。但是,我发现作为输入的对象不起作用-我必须具体一点,并且我只创建了一系列重载方法,这些重载方法在
Func
中使用不同的类型作为参数

在这种情况下,有没有办法使用对象作为输入?有没有更好的方法来做我想做的事?我试图坚持干,但我不知道如何利用FUNC来正确地做到这一点。我感谢任何帮助或指点

来自控制器的代码:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public JsonNetResult jsonUpdateProfile(UserProfile u)
    {
        JsonNetResult j = new JsonNetResult();
        var x = ModelState;
        Func<UserProfile, jsonResponseObj> updater = p => updateUserProfile(p);
        j.GetResponseObj(updater, u);

        return j;
    }

    private jsonResponseObj updateUserProfile(UserProfile u)
    {   
        UserProfile updateUser = db.UserProfiles.Find(u.UserId);
        updateUser = u;
        updateUser.UpdatedBy = u.UserName;
        updateUser.UpdatedOn = DateTime.Now;
        db.SaveChanges();
        return new jsonResponseObj(null,"Profile Updated");
    }

您应该将
GetResponseObj
方法更改为泛型:

    public void GetResponseObj<T>(Func<T, jsonResponseObj> custom, T arg) where T:class
    {
        try
        {
            Data = custom(arg);
        }
        catch (Exception ex)
        {
            // alert elmah
            Data = new jsonResponseObj(ex);
        }
    }

现在,您可以使用此属性装饰您的操作,并关闭try/catch块

谢谢@Kirill,我今天就试试这个!另外,@Kirill Bestemyanov-u errorHandlingService属性声明在哪里?我也不认为它是ProcessException方法中的参数。是否应该使用IActionFilter?不需要。您应该将其从语句中删除。我更新答案。
public class jsonResponseObj
{
    public string status { get; set; }
    public Object data { get; set; }
    public string msg { get; set; }

    public jsonResponseObj(Object data, string msg = "")
    {
        this.status = "OK";
        this.data = data;
        this.msg = msg;
    }

    public jsonResponseObj(Exception ex)
    {
        this.status = "ERROR";
        this.msg = ex.Message;
    }
}
    public void GetResponseObj<T>(Func<T, jsonResponseObj> custom, T arg) where T:class
    {
        try
        {
            Data = custom(arg);
        }
        catch (Exception ex)
        {
            // alert elmah
            Data = new jsonResponseObj(ex);
        }
    }
public class ExceptionToJsonNetResultAttribute
{
    public override void OnException(ExceptionContext filterContext)
    {
        if (filterContext.ExceptionHandled)
            return;

        if (!filterContext.HttpContext.Request.IsAjaxRequest())
            return;

        if (filterContext.Result is JsonNetResult)
            return;

        ProcessException(filterContext);
    }

    private static void ProcessException(ExceptionContext filterContext)
    {
        filterContext.HttpContext.Response.Clear();
        filterContext.HttpContext.Response.StatusCode = 500;
        filterContext.ExceptionHandled = true;
        filterContext.Result = new JsonResult
        {
            Data = new jsonResponseObj(filterContext.Exception);                
        };
        filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
    }
}