【问题标题】:How to get in Asp.Net Core a request POST from Angular如何在 Asp.Net Core 中获取来自 Angular 的请求 POST
【发布时间】:2019-11-14 14:41:56
【问题描述】:

在 Asp.NET MVC 中,我使用了以下过滤器:(效果很好)

public class TracerAttribute : ActionFilterAttribute
{
    public override Task OnActionExecutingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
    {
        return Task.Factory.StartNew(() =>
        {
            var path = ConfigurationManager.AppSettings["QueueAddress"];
            var queue = new MessageQueue(path);
            queue.DefaultPropertiesToSend.Recoverable = true;

            var assembly = System.Reflection.Assembly.GetExecutingAssembly();
            var body = TraceMessageHelper.BuildBody(actionContext, assembly);
            try
            {
                var label = ConfigurationManager.AppSettings["MessageLabel"] + body.Timestamp.ToString("yyyyMMddHHmmss");
                var message = new Message
                {
                    Formatter = new XmlMessageFormatter(new Type[] { typeof(TraceMessage) }),
                    Label = label,
                    Body = body
                };

                queue.Send(message);
            }
            catch (Exception e)
            {
                var logger = LogManager.GetLogger("LogInFile");
                logger.Warn(e, LogMessageHelper.FormatRequest("TRACE SEND FAILED", actionContext.Request));
                if (body != null)
                {
                    var tracerlogger = LogManager.GetLogger("TracerInFile");
                    tracerlogger.Info(JsonConvert.SerializeObject(body));
                }
            }

            queue.Close();
        });
    }
}

}

在 Asp.NET CORE 中,我使用以下过滤器:(不起作用)

public class TracerAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext actionContext)
    {            

        {
            var path = "FormatName:Direct=TCP:013cdnt2305\\private$\\TracerQueue";

            var queue = new MessageQueue(path);
            queue.DefaultPropertiesToSend.Recoverable = true;

            var assembly = System.Reflection.Assembly.GetExecutingAssembly();
            var body = TraceMessageHelper.BuildBody(actionContext, assembly);
            try
            {

                var label = "APR_USER_20191018132324";
                var message = new Message
                {
                    Formatter = new XmlMessageFormatter(new Type[] { typeof(TraceMessage) }),
                    Label = label,
                    Body = body
                };

                queue.Send(message);
            }
            catch (Exception e)
            {
                HttpRequestMessageFeature hreqmf = new HttpRequestMessageFeature(actionContext.HttpContext);

                var logger = LogManager.GetLogger("LogInFile");


                logger.Warn(e, LogMessageHelper.FormatRequest("TRACE SEND FAILED", hreqmf.HttpRequestMessage));
                if (body != null)
                {
                    var tracerlogger = LogManager.GetLogger("TracerInFile");
                    tracerlogger.Info(JsonConvert.SerializeObject(body));
                }
            }

            queue.Close();
        }
    }
}

}

在 ASP.net Core 中我只需要使用一个参数进行更改

在启动中

    public void ConfigureServices(IServiceCollection services)
    {

        services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
        {
            builder.AllowAnyOrigin()
                   .AllowAnyMethod()
                   .AllowAnyHeader();
        }));

        services.AddControllersWithViews();
        services.AddRazorPages();

        services.AddMvc(config =>
              {
                  config.Filters.Add(new TracerAttribute());
              });

        //use our filter as a service type on the Action or Controller level
        //services.AddScoped<TracerAttribute>();

    }

在 ASP.NET MVC 中(工作正常)

在 ASP.NET CORE 中(不起作用)

两者都有角度回报:

谁能帮我解决一下?

【问题讨论】:

    标签: c# angular asp.net-core filter


    【解决方案1】:

    如果 Angular 应用程序将 json 数据(Content-Type: application/json) 发布到 .net 核心服务器端,您可以通过 [FromBody] 获取值:

    [TracerAttribute]
    [HttpPost]
    public IActionResult MyAction([FromBody]Mymodel mymodel)
    {
    
    }
    

    当调用动作过滤器时,主体流已经被读取并且 [FromBody] 模型已经被填充。然后您可以通过以下方式获取数据:

    public class TracerAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext actionContext)
        {
            var modelValues = actionContext.ActionArguments["mymodel"] as Mymodel ;
    
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-10-27
      • 1970-01-01
      • 1970-01-01
      • 2018-04-15
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 2021-03-26
      相关资源
      最近更新 更多