在处理asp.net的请求之前,有没有办法在不使用ActionFilter
的情况下捕获所有传入控制器的请求?
答案 0 :(得分:-1)
在您的Startup.cs
文件中,将以下代码放入Configure()
方法中:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
//your code below
app.Run(async (context) =>
{
string body;
using (Stream receiveStream = context.Request.Body)
{
using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
{
body = readStream.ReadToEnd();
}
}
Console.WriteLine(body.ToString());
});
}
然后请注意,app.UseMvc()
始终具有优先级,这意味着您的应用程序中指定的任何路由仅会到达控制器。因此,要么指定其他路由,要么不使用MVC中的路由,而是将路由放入app.Run(async (context)