繁体   English   中英

如何在 Asp.Net Core 中获取来自 Angular 的请求 POST

[英]How to get in Asp.Net Core a request POST from Angular

在 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 核心中,我只需要使用一个参数进行更改

在启动中

    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 中(工作正常) 从 Json 中的 Angular JS 返回

在 ASP.NET CORE(不工作) 从 Json 中的 Angular JS 返回

Angular 返回:

在此处输入图像描述

任何人都可以帮我解决它吗?

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

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

}

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

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


    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM