【问题标题】:How do I centralize code shared by multiple Web API controllers?如何集中由多个 Web API 控制器共享的代码?
【发布时间】:2023-04-02 08:25:01
【问题描述】:

我目前有近 10 个控制器共享相同的代码。代码很简单,它只是检查一组数据是否为空,并检查当前用户是否有权访问数据。

如果有问题,我会抛出 HttpResponseException。

代码在每个控制器中都可以工作。我还设法集中了代码,但我认为我这样做的方式是错误的。我创建了一个继承 ApiController 的新类,然后我让控制器继承了我的新类。这是我可以让 HttpResponseExceptions 工作的唯一方法。代码如下:

//New centralized class:

public class AuthorizationClass : ApiController
{   
    private DataModel db = new DataModel();

    public async Task checkUserisValid(int user_id)
    {
        user_list user_list = await db.user_list.FindAsync(user_id);

        if (user_list == null)
        {
            throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest,"This user does not exist"));
        }

        int businessID = user_list.business_id;

        var result = checkAccess(User.Identity.Name, businessID);

        if (result.Count <= 0)
        {
            throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "You do not have access to modify this business"));
        }
    }

    public static List<user_details> checkAccess(string userName, int id)
    {
        //code which checks if the user is in the right tables
            return checkAccess.ToList();
    }
}

然后在控制器类中,我有:

    public class MyController : AuthorizationClass 
{
        public async Task<IHttpActionResult> Postnew_table(int id, new_table new_table)
        {
            await checkUserisValid(id);

        //rest of controller    
            }
}   

我尝试以不同的方式来做,但这是我可以让它与 HttpResponseException 一起工作的唯一方法。有没有更好的方法可以在不继承类的情况下做到这一点,还是这是我追求的唯一方法?

谢谢。

【问题讨论】:

  • 这不是经典的 ASP。看起来可能是asp.net mvc
  • 抱歉,我用错了标签!
  • 为什么这段代码必须在控制器中?为什么没有一个共同的组装?但我也看不出有什么问题,每个控制器都会继承 AuthorizationClass 吗?我想这行得通。
  • 为什么?也许您应该将 dbcontext 作为参数传递?让它工作。
  • 然后将其作为参数传入。

标签: asp.net asp.net-web-api asp.net-web-api2 code-reuse


【解决方案1】:

您可以将这两个方法移动到一个通用程序集中的某个静态帮助程序类中,您提到Request 是控制器上的一个实例变量,只需将其传递给该方法即可。

public static class SomeHelper
{
    public static async Task checkUserisValid(int user_id, DataModel db, Request request, User user)
    {
       user_list user_list = await db.user_list.FindAsync(user_id);

       if (user_list == null)
       {
          throw new   HttpResponseException(request.CreateErrorResponse(HttpStatusCode.BadRequest,"This user does not exist"));
        }

        int businessID = user_list.business_id;

        var result = checkAccess(user.Identity.Name, businessID);

        if (result.Count <= 0)
        {
          throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "You do not have access to modify this business"));
        }
   }

  public static List<user_details> checkAccess(string userName, int id)
  {
      //code which checks if the user is in the right tables
          return checkAccess.ToList();
   }

}

【讨论】:

    猜你喜欢
    • 2021-12-25
    • 2016-11-05
    • 2013-06-19
    • 1970-01-01
    • 2018-11-24
    • 2018-08-21
    • 1970-01-01
    • 1970-01-01
    • 2011-03-16
    相关资源
    最近更新 更多