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

时间:2018-02-22 16:59:09

标签: c# asp.net asp.net-mvc asp.net-mvc-routing attributerouting

我有一个用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或者没有,我想执行不同的操作。

2 个答案:

答案 0 :(得分:1)

只需在行动中接受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);
}

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

答案 1 :(得分:0)

您可以创建一个这样的私有方法:

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);
}