在哪裏獲取mvc
有三種方案ide
1.1. 經過自定義Controller基類ui
優勢: 簡單,統一 缺點: 須要集成基類, 若是出現原始提交數據沒法映射到Action參數裏的狀況,沒法獲取原始映射數據。 `csharp public class BaseController : Controller {
public override void OnActionExecuting(ActionExecutingContext context) { base.OnActionExecuting(context);url
var request = context.HttpContext.Request; string url = request.GetDisplayUrl(); //using Microsoft.AspNetCore.Http.Extensions; StringBuilder sb = new StringBuilder(); sb.AppendLine(url); if (request.ContentType?.StartsWith("multipart/form-data;") == true) { sb.AppendLine("Form Parameters: "); sb.AppendLine(JsonConvert.SerializeObject(request.Form)); } if(context.ActionArguments.Count > 0) { sb.AppendLine("JSON Parameters: "); sb.AppendLine(JsonConvert.SerializeObject(context.ActionArguments)); } } }
` 繼承此基類的controller中,均可以獲取和處理請求信息。code
1.2. 經過FilterAttributeorm
優勢:可定製性強 缺點:沒法統一處理中間件
1.3. 經過中間件繼承
優勢:能夠統一處理,功能強 缺點:未分析 ` private string ReadBodyAsString(HttpRequest request) { var initialBody = request.Body; // Workaroundip
try { request.EnableRewind(); using (StreamReader reader = new StreamReader(request.Body)) { string text = reader.ReadToEnd(); return text; } } finally { // Workaround so MVC action will be able to read body as well request.Body = initialBody; } return string.Empty; }
`string
上面的代碼已經展現了,須要引用 Microsoft.AspNetCore.Http.Extensions;
而後利用其中 HttpRequest 的擴展方法: GetDisplayUrl()