Asp.Net WebApi与BaseController的继承

时间:2015-01-15 13:07:21

标签: c# asp.net .net asp.net-web-api asp.net-web-api2

我目前正在努力在WebApi中继承我的工作,就像我一样。 也许你可以指出我正确的方向?

我的目标是拥有一个通用的WebApi项目,该项目可以在多个其他WebApi项目中重复使用。

例如:我有一个可重用的UserController,可以处理多个WebSites的登录。现在我构建一个新的WebSite,并将UserController简单地添加到新创建的项目中,这项工作很简单。

但是现在对新WebSite的要求发生了变化,我需要重载UserController方法以反映这些变化,而无需修改通用控制器。

我想要实现的内容可以在下面的代码片段中看到(当然这不会编译,因为C#抱怨不适合重载的方法。)

// This belongs to solution "GenericWebApi"

public class LoginRequest
{
    public string User { get; set; }
}

public class BaseApiController : ApiController
{
     [Route("login")]
    public virtual HttpResponseMessage Login(LoginRequest request) 
    {
        return new HttpResponseMessage();
    }
}


// This belongs to solution "CarWebsite"

public class CarWebsiteLoginRequest : LoginRequest
{
    public string Car { get; set; }
}

[RoutePrefix("api/users")]
public class CarWebsiteApiController : BaseApiController
{
    public override HttpResponseMessage Login(CarWebsiteLoginRequest request)
    {
        // Do some car specific login stuff
        return base.Login(request);
    }
}

我找到的一个解决方案是取代" LoginRequest"和" CarWebsiteLoginRequest"使用"动态",但这感觉完全错误,而ModelBinder当然不会按预期工作。

你有什么想法可以解决这个问题吗?

提前致谢

2 个答案:

答案 0 :(得分:7)

您可以将BaseApiController通用:

public class BaseApiController<TLoginRequest> : ApiController
where TLoginRequest : LoginRequest
{
    [Route("login")]
    public virtual HttpResponseMessage Login(TLoginRequest request)
    {
        return new HttpResponseMessage();
    }
}

并使CarWebsiteApiController使用CarWebsiteLoginRequest作为通用参数:

public class CarWebsiteApiController : BaseApiController<CarWebsiteLoginRequest>
{
    public override HttpResponseMessage Login(CarWebsiteLoginRequest request)
    {
        // Do some car specific login stuff
        return base.Login(request);
    }
}

答案 1 :(得分:0)

如果您拉出变量并在方法中读取它,则更容易超载

    /// <summary>
/// Notice name is not Controller.cs  This would expose this API if the name ended in controller
/// </summary>
public class GenericControllerApi : ApiController
{
    protected JavaScriptSerializer JsonReader = new JavaScriptSerializer();

    [HttpPost]
    public virtual HttpResponseMessage Login()
    {
        string JsonRequest = Request.Content.ReadAsStringAsync().Result;

        LoginRequest JSONData = null;
        try
        {
            JSONData = (LoginRequest)JsonReader.Deserialize(JsonRequest, typeof(LoginRequest));
        }
        catch { }

        if (JSONData != null)
        {
            if (CommonUserCalls(JSONData.User))
            {
                ShowUser UserReturn = new ShowUser();
                UserReturn.UserName = "BaseUser";
                return Request.CreateResponse(HttpStatusCode.OK, UserReturn);
            }

            return Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Invalid User");
        }
        else
        {
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "ShowUser not provided");
        }
    }

    protected bool CommonUserCalls(string User)
    {
        if (User == "MyUser")
        {
            return true;
        }
        return false;
    }

    public class LoginRequest
    {
        public string User { get; set; }
    }

    protected class ShowUser
    {
        public string UserName { get; set; }
    }
}

public class CarController : GenericControllerApi
{
    [HttpPost]
    public override HttpResponseMessage Login()
    {
        string JsonRequest = Request.Content.ReadAsStringAsync().Result;

        CarWebsiteLoginRequest JSONData = null;
        try
        {
            JSONData = (CarWebsiteLoginRequest)JsonReader.Deserialize(JsonRequest, typeof(CarWebsiteLoginRequest));
        }
        catch { }

        if (JSONData != null)
        {
            if (CommonUserCalls(JSONData.User))
            {
                ShowUser UserReturn = new ShowUser();
                UserReturn.UserName = "CarUser";
                return Request.CreateResponse(HttpStatusCode.OK, UserReturn);
            }

            return Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Invalid User");
        }
        else
        {
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "CarWebsiteLoginRequest not provided");
        }
    }

    public class CarWebsiteLoginRequest : LoginRequest
    {
        public string Car { get; set; }
    }
}

假设您正在阅读/发送JSON