这些代码通过用户数据报协议提供发送数据.下面有两个代码.当我使用第一个代码来获取无法访问的IP地址时,我得到了三秒钟的延迟.
请查看新结果标题
刚刚打开新的C#CONSOLE APP并将这些代码粘贴在其中.(第一个代码)
using System;
using System.Net;
using System.Net.Sockets;
namespace Test
{
class Program
{
static void Main(string[] args)
{
byte[] data = { 1, 20, 60, 44, 244 };
while (true)
{
Console.WriteLine(DateTime.Now.ToString("h:mm:ss tt"));
try
{
using (var client = new UdpClient())
{
// Please check IP Address, It must be unreachable...
// IPEndPoint ep = new IPEndPoint(IPAddress.Parse("192.168.1.141"), 55600);
// client.Connect(ep);
client.Send(data, data.Length, "192.168.1.141" , 55600);
}
Console.WriteLine(DateTime.Now.ToString("h:mm:ss tt"));
Console.WriteLine(" ");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
} …
Run Code Online (Sandbox Code Playgroud) 我创建了一个custom AuthorizeAttribute
验证一些OAuth credentials of user
.
一旦我获得有效的用户,我想将响应数据返回到控制器,如何在 web api .net 中实现此目的。
public class CustomAttribute : AuthorizeAttribute
{
protected override bool IsAuthorized(HttpActionContext actionContext)
{
var response = mydata.Result.Content.ReadAsStringAsync();
if (mydata.Result.StatusCode == HttpStatusCode.OK)
{
// return response data to controller
return true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我搜了一下我得到in mvc
了can be done like below
。
public class CustomAttribute : AuthorizeAttribute
{
public string BlackListedUsers { get; set; }
protected override bool AuthorizeCore(AuthorizationContext filterContext)
{
filterContext.HttpContext.Items["test"] = "foo";
return true; …
Run Code Online (Sandbox Code Playgroud) 我找到了以下代码片段,并想知道该Func<object> getObject
属性的目的是什么:
public void InsertCacheItem(string key, Func<object> getObject, TimeSpan duration)
{
try
{
var value = getObject();
if (value == null)
return;
key = GetCacheKey(key);
_cache.Insert(
key,
value,
null,
System.Web.Caching.Cache.NoAbsoluteExpiration,
duration,
System.Web.Caching.CacheItemPriority.Normal,
null);
}
catch { }
}
Run Code Online (Sandbox Code Playgroud)
如何通过传递Func属性来调用此特定函数?