小编Pri*_*iya的帖子

从htmldocument中删除html节点:HTMLAgilityPack

在我的代码中,我想删除没有src值的img标记.我正在使用HTMLAgilitypack的HtmlDocument对象.我发现img没有src值并试图删除它..但它给了我错误集合被修改; 枚举操作可能无法执行.任何人都可以帮助我吗?我使用的代码是:

foreach (HtmlNode node in doc.DocumentNode.DescendantNodes())
{
    if (node.Name.ToLower() == "img")
    {                            
           string src = node.Attributes["src"].Value;
           if (string.IsNullOrEmpty(src))
           {
               node.ParentNode.RemoveChild(node, false);    
           }
   }
   else
   {
             ..........// i am performing other operations on document
   }
}
Run Code Online (Sandbox Code Playgroud)

c# iteration collections dom html-agility-pack

10
推荐指数
2
解决办法
2万
查看次数

日期时间格式问题:字符串未被识别为有效的DateTime

我想在C#中将输入字符串格式化为MM/dd/yyyy hh:mm:ss格式.
输入字符串的格式MM/dd/yyyy hh:mm:ss
为例如:"04/30/2013 23:00"

我试过Convert.ToDateTime()功能,但它认为4是日期,3是月,这不是我想要的.实际上月是04,日期是03.

DateTime.ParseExact()也试过功能,但是得到了Exception.

我收到错误:

字符串未被识别为有效的DateTime.

c# asp.net datetime

9
推荐指数
2
解决办法
13万
查看次数

百分比在将特殊字符发送到URL之前对其进行编码


我需要传递#,!等特殊字符 在Facebook,Twitter和此类社交网站的URL中等.为此,我用URL Escape Codes替换这些字符.

 return valToEncode.Replace("!", "%21").Replace("#", "%23")
   .Replace("$", "%24").Replace("&", "%26")
   .Replace("'", "%27").Replace("(", "%28")
   .Replace(")", "%29").Replace("*", "%2A");
Run Code Online (Sandbox Code Playgroud)

它适用于我,但我想更有效地做它.有没有其他方法来逃避这些角色?我尝试使用Server.URLEncode(),但Facebook没有呈现它.

先谢谢,
Priya

c# asp.net urlencode url-encoding

9
推荐指数
3
解决办法
2万
查看次数

不同NameSpace ASP.NET WEB API中具有相同名称的控制器

我需要在不同的命名空间中具有相同名称的控制器.我拥有的控制器是:

namespace BSB.Messages.Controllers.V1
{    
    public class MessagesController : ApiController {...}
}

namespace BSB.Messages.Controllers.V2
{       
    public class MessagesController : ApiController {...}
}
Run Code Online (Sandbox Code Playgroud)

我尝试在启动时配置它.但是当我打电话时,它显示错误:

找到了多个匹配名为"messages"的控制器的类型.如果为此请求提供服务的路由('api/{namespace}/{controller}/{action}/{id}')发现多个控制器使用相同的名称但不同的命名空间定义,则不会发生这种情况,这是不受支持的

我的注册功能WebApiConfig是:

public static void Register(HttpConfiguration config)
{
        config.MapHttpAttributeRoutes();
        config.Routes.MapHttpRoute("DefaultApi", "api/{namespace}/{controller}/{action}/{id}", new { id = UrlParameter.Optional });
}
Run Code Online (Sandbox Code Playgroud)

我的RegisterRoutes功能是:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    var r = routes.MapRoute(
                name: "Default",
                url: "v1/messages/{action}/{id}",
                defaults: new { id = UrlParameter.Optional },
                 namespaces: new[] { "BSB.Messages.Controllers.V1" }

            );
    r.DataTokens["Namespaces"] = new string[] { "BSB.Messages.Controllers.V1" }; …
Run Code Online (Sandbox Code Playgroud)

c# routes asp.net-web-api asp.net-web-api-routing

8
推荐指数
1
解决办法
3429
查看次数

类型“System.Int16”的对象无法转换为类型“System.Nullable`1[System.Int32]

我有一个方法可以从数据读取器的数据生成类类型列表。

if (datareader != null && datareader .HasRows)
{
  Dictionary<string, PropertyInfo> pDict= GetPropertyDictionary<T>();
  var fields = GetFieldNames(datareader );
  while (datareader .Read())
  {
    T myobj= new T();
    for (int index = 0; index < fields.Count; index++)
    {                        
      if (pDict.TryGetValue(fields[index], out PropertyInfo info))
      {
        var val1 = datareader .GetValue(index);                                
        info.SetValue(myobj, (val1 == DBNull.Value) ? null : val1, null);
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我有类属性,其中一些可以为空。

public string StudentName{ get; set; }
public decimal? percentage{ get; set; }
public int? StudentNumber{ get; set; }
Run Code Online (Sandbox Code Playgroud)

代码适用于除 …

c# nullable datareader system.reflection

6
推荐指数
1
解决办法
2万
查看次数

具有动态配置的 HttpClientFactory

我正在开发 .net core web 应用程序,它需要以特定的时间间隔(每 2 分钟,20 次不同的 API 调用,我需要向最终用户显示 API 结果)调用远程 API ,托管有4 个不同的域名

我使用 HttpClient 来调用远程 API。但是随着用户的增加,我的 CPU 使用率增加了 40%。我怀疑 HttpClient 可能是原因。在浏览了几篇博客之后,我正在尝试使用 HttpClientFactory。
我有一个从 Controller Action 调用的方法,我需要根据几个参数动态识别 BaseUrl。目前我在 StartUp.cs 中创建了 4 个 NamedClients,如下所示:

   services.AddHttpClient(ApiConfig.NamedHttpClients.TestUrl1, client =>
       {
           client.BaseAddress = new Uri(Configuration.GetSection("BaseUrls").GetSection("TestUrl1").Value);
           client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
           client.DefaultRequestHeaders.Add("Authorization", "Basic " + ApiConfig.GetEncodedCredentials(Configuration));
           var userAgent = "C# app";
           client.DefaultRequestHeaders.Add("User-Agent", userAgent);
       }).SetHandlerLifetime(TimeSpan.FromMinutes(5));
        services.AddHttpClient(ApiConfig.NamedHttpClients.TestUrl2, client =>
        {
            client.BaseAddress = new Uri(Configuration.GetSection("BaseUrls").GetSection("TestUrl2").Value);
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Add("Authorization", "Basic " + ApiConfig.GetEncodedCredentials(Configuration));
            var userAgent …
Run Code Online (Sandbox Code Playgroud)

asp.net-core-2.1 httpclientfactory .net-core-2.1

5
推荐指数
0
解决办法
880
查看次数

将日期时间显示为MM/dd/yyyy HH:mm格式c#

在数据库中,datetime存储在MM-dd-yyyy HH:mm:ss fromat中.
但是,我想以"MM/dd/yyyy HH:mm"格式显示日期时间.我通过使用String.Format()尝试了它.

txtCampaignStartDate.Text = String.Format("{0:MM/dd/yyyy 
                           HH:mm}",appCampaignModel.CampaignStartDateTime);
Run Code Online (Sandbox Code Playgroud)

appCampaignModel.CampaignStartDateTime是DateTime对象,其值为"MM-dd-yyyy HH:mm"格式.

我想以"MM/dd/yyyy HH:mm"格式显示.

任何人都可以帮助我吗?

c# asp.net datetime

2
推荐指数
2
解决办法
3万
查看次数