如何在此控制器中正确使用可选路由参数来简化它?

How to correctly use optional route parameters in this controller to simplify it?

我有一个用 ASP.NET 制作的控制器,我真的想通过快速查看来简化它:

// REST representation of Storage
// There is always at least two options to view them
// Data as is or Quick view at metrics averages
[Route("metrics")]
public class MetricsController : Controller
{
    // Get raw Storage object
    [HttpGet]
    public IActionResult GetStorageView()
    {   
        // TODO: do not use in production
        WSManModule.HyperVMetric.test(false);
        //

        var response = MetricsService.Instance.GetRawMetrics();

        if (response == null)
        {
            return NotFound();
        }

        if (Request.QueryString.Value == "?q=quick")
        {
            return Ok(new StorageQuickView(response));
        }

        return Ok(response);
    }

    // Get metrics for specific device
    [HttpGet("{deviceName}")]
    public IActionResult GetDeviceView(string deviceName)
    {
        var response = MetricsService.Instance.GetDeviceMetrics(deviceName);

        if (response == null)
        {
            return NotFound();
        }

        if (Request.QueryString.Value == "?q=quick")
        {
            return Ok(new DeviceQuickView(response));
        }

        return Ok(response);
    }

    // Get metrics for specific component within the device
    [HttpGet("{deviceName}/{componentName}")]
    public IActionResult GetComponentView(string deviceName, string componentName)
    {
        var response = MetricsService.Instance.GetComponentMetrics(deviceName, componentName);

        if (response == null)
        {
            return NotFound();
        }

        if (Request.QueryString.Value == "?q=quick")
        {
            return Ok(new ComponentQuickView(response));
        }

        return Ok(response);
    }
}

现在它确实有很多重复,我不喜欢它。 有没有什么办法可以使用 {quick?} 或类似的可选参数来正确地做到这一点?

简单地说:如果我们在路线的尽头有/quick或没有

,我想执行不同的操作

只需在您的操作中接受 q 参数:

// Get raw Storage object
[HttpGet]
public IActionResult GetStorageView(string q)
{   
    // TODO: do not use in production
    WSManModule.HyperVMetric.test(false);
    //

    var response = MetricsService.Instance.GetRawMetrics();

    if (response == null)
    {
        return NotFound();
    }

    if (q == "quick")
    {
        return Ok(new StorageQuickView(response));
    }

    return Ok(response);
}

// Get metrics for specific device
[HttpGet("{deviceName}")]
public IActionResult GetDeviceView(string deviceName, string q)
{
    var response = MetricsService.Instance.GetDeviceMetrics(deviceName);

    if (response == null)
    {
        return NotFound();
    }

    if (q == "quick")
    {
        return Ok(new DeviceQuickView(response));
    }

    return Ok(response);
}

操作方法参数不是只是从路由派生的。这些值来自 ,默认提供程序之一解析查询字符串。因此,您只需将查询字符串值添加到操作方法参数中,而无需手动解析或比较查询字符串。

你可以像这样创建一个私有方法:

private IAction ProcessResponse<T>(IMyResponseType response)
{
    if(response == null)
    {
        return NotFound();
    }

    if (Request.QueryString.Value == "?q=quick")
    {
        var okInstance = (T) Activator.CreateInstance(typeof (T), response);
        return Ok(okInstance);
    }

    return Ok(response);
}

并像这样使用它:

// Get metrics for specific component within the device
[HttpGet("{deviceName}/{componentName}")]
public IActionResult GetComponentView(string deviceName, string componentName)
{
    var response = MetricsService.Instance.GetComponentMetrics(deviceName, componentName);

    return ProcessResponse<ComponentQuickView>(response);
}

// Get raw Storage object
[HttpGet]
public IActionResult GetStorageView()
{   
    // TODO: do not use in production
    WSManModule.HyperVMetric.test(false);
    //

    var response = MetricsService.Instance.GetRawMetrics();

    return ProcessResponse<StorageQuickView>(response);
}