是否可以获取Web API控制器的构造函数中的头信息?我想根据标头值设置变量,但我不想对每个方法都这样做.我对自定义标头值特别感兴趣,但此时我会满足授权标准.我可以让它在一个工作,AuthorizationFilterAttribute但我也需要它在控制器级别.
[PolicyAuthorize]
public class PoliciesController : ApiController
{
public PoliciesController()
{
var x = HttpContext.Current; //will be null in constructor
}
public HttpResponseMessage Get()
{
var x = HttpContext.Current; //will be available but too late
}
}
public class PolicyAuthorizeAttribute : AuthorizationFilterAttribute
{
public override void OnAuthorization(HttpActionContext actionContext)
{
var authHeader = actionContext.Request.Headers.Authorization; //can get at Authorization header here but no HTTPActionContext in controller
}
}
Run Code Online (Sandbox Code Playgroud) 我想遍历我的类的属性并获取每个属性类型.我大部分时间都得到了它,但是在尝试获取类型时,而不是获取字符串,int等,我得到类型反射.有任何想法吗?如果需要更多背景信息,请与我们联系.谢谢!
using System.Reflection;
Type oClassType = this.GetType(); //I'm calling this inside the class
PropertyInfo[] oClassProperties = oClassType.GetProperties();
foreach (PropertyInfo prop in oClassProperties) //Loop thru properties works fine
{
if (Nullable.GetUnderlyingType(prop.GetType()) == typeof(int))
//should be integer type but prop.GetType() returns System.Reflection
else if (Nullable.GetUnderlyingType(prop.GetType()) == typeof(string))
//should be string type but prop.GetType() returns System.Reflection
.
.
.
}
Run Code Online (Sandbox Code Playgroud) Shell示例如下.基本上,我希望客户和员工从IPerson实施SSN属性.但是,我希望客户得到并设置(这不是问题),但我希望员工只得到.
Public Interface IPerson
Property SSN As String
End Interface
Public Class Client
Implements IPerson
Public Property SSN As String Implements AELName.IPerson.SSN
Get
Return _SSN
End Get
Set(value As String)
_SSN = value
End Set
End Property
End Class
Public Class Employee
Implements IPerson
Public Readonly Property SSN As String Implements AELName.IPerson.SSN
Get
Return _SSN
End Get
End Property
End Class
Run Code Online (Sandbox Code Playgroud)
员工生成错误"'SSN'无法实现'SSN',因为接口'IPerson'上没有匹配的属性".是否有一种简单的方法来覆盖Employee的SSN实现?
有人可以仔细检查我修剪过的例子吗?更新Documents表时,我希望它更新Queue表上的EntryDate.但是,我不希望队列表上的触发器仅针对此进程触发.这意味着,如果某个其他进程在此进程运行时更新Queue表上的EntryDate,我将希望队列表上的触发器为该特定事务触发.我不确定是否需要为下面的代码执行任何类型的锁定,以确保没有其他进程踩到它的脚趾.谢谢!
create trigger [dbo].[Documents_trigUpdate] on [dbo].[Documents]
for update
as
begin transaction
alter table [Queue] disable trigger Queue_trigUpdate
update [Queue] set EntryDate = getdate()
alter table [Queue] enable trigger Queue_trigUpdate
commit transaction
go
Run Code Online (Sandbox Code Playgroud) C#的新手,所以我遇到了一个简短的陈述问题.我想转换......
if (m_dtLastLogin == null)
drow["LastLogin"] = DBNull.Value;
else
drow["LastLogin"] = m_dtLastLogin;
Run Code Online (Sandbox Code Playgroud)
至
drow["LastLogin"] = (m_dtLastLogin == null) ? System.DBNull.Value : m_dtLastLogin;
Run Code Online (Sandbox Code Playgroud)
长版本工作得很好,但是,简写版本会生成错误"无法确定条件类型,因为'System.DBNull'和'System.DateTime?'之间没有隐式转换." 我的支持代码基本上是......
private DateTime? m_dtLastLogin;
m_dtLastLogin = null;
DataRow drow;
drow = m_oDS.Tables["Users"].Rows[0];
Run Code Online (Sandbox Code Playgroud)
有人可以用这个简短的手帮我吗?
c# ×3
properties ×2
class ×1
if-statement ×1
interface ×1
overriding ×1
reflection ×1
sql ×1
sql-server ×1
transactions ×1
vb.net ×1