我'创建一个日志,我需要检索请求体以保存在db中.我用HttpActionContext创建了一个过滤器.我尝试通过filterContext.Request.Content.ReadAsStringAsync()恢复.但它总是给我一个空字符串.
LogFilter.cs
public override void OnActionExecuting(HttpActionContext filterContext)
{
try
{
Task<string> content = filterContext.Request.Content.ReadAsStringAsync();
string body = content.Result;
logModel.RequestLog rl = new logModel.RequestLog();
rl.IP = ((HttpContextWrapper)filterContext.Request.Properties["MS_HttpContext"]).Request.UserHostAddress;
rl.Type = filterContext.ControllerContext.RouteData.Values["controller"].ToString().ToUpper();
rl.URL = filterContext.Request.RequestUri.OriginalString;
rl.Operation = filterContext.Request.Method.Method;
rl.RequestDate = DateTime.Now;
filterContext.ControllerContext.RouteData.Values.Add("reqID", new deviceLog.RequestLog().Add(rl).ID.ToString());
}
catch { }
//return new deviceLog.RequestLog().Add(rl);
base.OnActionExecuting(filterContext);
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试从DataTableSQL Server 中的表中批量插入,但在我的数据表中没有 id 列。当我调用该WriteToServer方法时,它会抛出异常并显示消息
无法在列 id 中插入 null
代码:
CREATE TABLE [dbo].[TBL_HISTORY]
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[VAR1] [int] NOT NULL,
[VAR2] [datetime] NOT NULL,
[VAR3] [smallint] NOT NULL
)
Run Code Online (Sandbox Code Playgroud)
我的数据表有以下数据
[VAR1], [VAR2], [VAR3]
Run Code Online (Sandbox Code Playgroud)
C#代码:
using(SqlBulkCopy bulk = new SqlBulkCopy(this.mConnection,SqlBulkCopyOptions.UseInternalTransaction | SqlBulkCopyOptions.KeepIdentity, null))
{
bulk.DestinationTableName = ToTable;
bulk.WriteToServer(dt); // here exception is thrown
}
Run Code Online (Sandbox Code Playgroud)