請問我在.Net Core Startup.cs中使用了app.UseExceptionHandler("/Home/Error");做為所有Controller api的Exception Handler。
我目前在HomeController.Error可以透過HttpContext.Features.RawTarget取得發生Exception的API method name,但我要怎麼取得傳入此method的Arguments name & value呢?
如下例:
public IActionResult GetGroup([FromForm] string empCode, [FromForm] string groupCode)
{
throw new Exception("System testing...")
return Ok(returnModel);
}
我要在下方Error method取得empCode及groupCode的值
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
var exception = HttpContext.Features.Get<IExceptionHandlerFeature>();
//這裡要怎麼動態取得Exception method的arguments,以上例言就是取得empCode及groupCode的參數名及參數值
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
試試下面的方法,我自己測試有成功抓到。
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
var collection = HttpContext.Request.Form;
string response = string.Join(",", collection.Select(kv => kv.Key + ":" + kv.Value).ToArray());
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
結果顯示: