C# 如何正确使用此控制器中的可选路由参数来简化它?
我有一个用ASP.NET制作的控制器,我真的想用quick view简化它:C# 如何正确使用此控制器中的可选路由参数来简化它?,c#,asp.net,asp.net-mvc,asp.net-mvc-routing,attributerouting,C#,Asp.net,Asp.net Mvc,Asp.net Mvc Routing,Attributerouting,我有一个用ASP.NET制作的控制器,我真的想用quick view简化它: // 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
// 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);
}
private IAction ProcessResponse(IMyResponseType响应)
{
如果(响应==null)
{
返回NotFound();
}
if(Request.QueryString.Value==“?q=quick”)
{
var okInstance=(T)Activator.CreateInstance(typeof(T),response);
返回Ok(正常状态);
}
返回Ok(响应);
}
然后像这样使用它:
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);
}
//获取设备中特定组件的指标
[HttpGet(“{deviceName}/{componentName}”)]
public IActionResult GetComponentView(字符串deviceName、字符串componentName)
{
var response=MetricsService.Instance.GetComponentMetrics(deviceName,componentName);
返回ProcessResponse(response);
}
//获取原始存储对象
[HttpGet]
公共IActionResult GetStorageView()
{
//TODO:请勿在生产中使用
WSManModule.HyperVMetric.test(false);
//
var response=MetricsService.Instance.GetRawMetrics();
返回ProcessResponse(response);
}
如果这是asp核心,在控制器使用中间件处理响应后,您可以执行一些逻辑。我在网上也发现了[FromQuery],这和你的有什么不同?[FromQuery]
和其他类似的属性允许你指定从哪个值提供者获取值,覆盖了按注册顺序尝试的默认行为。但是,AFAIK属性在MVC5中不可用,仅在WebAPI和ASP.NETCore中可用。