ASP.NET常见异常处理示例

时间:2023-12-24 16:23:19
  • 将指定的年月日转初始化为DateTime新的实例(Nop.Admin.Controllers.OrderController.ParseProductAttributes)
    case AttributeControlType.Datepicker:
    {
    var day = form[controlId + "_day"];
    var month = form[controlId + "_month"];
    var year = form[controlId + "_year"];
    DateTime? selectedDate = null;
    try
    {

    selectedDate = new DateTime(Int32.Parse(year), Int32.Parse(month), Int32.Parse(day));
    }
    catch { }
    if (selectedDate.HasValue)
    {
    attributesXml = _productAttributeParser.AddProductAttribute(attributesXml,
    attribute, selectedDate.Value.ToString("D"));
    }
    }
    break;
  • 能否出订单信息(\src\Presentation\Nop.Web\Administration\Controllers\OrderController.cs(1346))
try
{
byte[] bytes = _exportManager.ExportOrdersToXlsx(orders);
return File(bytes, MimeTypes.TextXlsx, "orders.xlsx");
}
catch (Exception exc)
{
ErrorNotification(exc);
return RedirectToAction("List");
}
  • 发送HTTP请求(\src\Plugins\Nop.Plugin.ExchangeRate.EcbExchange\EcbExchangeRateProvider.cs(60))
    //get exchange rates to euro from European Central Bank
    var request = (HttpWebRequest)WebRequest.Create("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml");
    try
    {
    using (var response = request.GetResponse())
    {
    //load XML document
    var document = new XmlDocument();
    document.Load(response.GetResponseStream()); //add namespaces
    var namespaces = new XmlNamespaceManager(document.NameTable);
    namespaces.AddNamespace("ns", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref");
    namespaces.AddNamespace("gesmes", "http://www.gesmes.org/xml/2002-08-01"); //get daily rates
    var dailyRates = document.SelectSingleNode("gesmes:Envelope/ns:Cube/ns:Cube", namespaces);
    DateTime updateDate;
    if (!DateTime.TryParseExact(dailyRates.Attributes["time"].Value, "yyyy-MM-dd", null, DateTimeStyles.None, out updateDate))
    updateDate = DateTime.UtcNow; foreach (XmlNode currency in dailyRates.ChildNodes)
    {
    //get rate
    decimal currencyRate;
    if (!decimal.TryParse(currency.Attributes["rate"].Value, out currencyRate))
    continue; ratesToEuro.Add(new Core.Domain.Directory.ExchangeRate()
    {
    CurrencyCode = currency.Attributes["currency"].Value,
    Rate = currencyRate,
    UpdatedOn = updateDate
    });
    }
    }
    }
    catch (WebException ex)
    {
    _logger.Error("ECB exchange rate provider", ex);
    }
  • EF操作(\src\Libraries\Nop.Data\EfRepository.cs(182))
    try
    {
    if (entities == null)
    throw new ArgumentNullException("entities"); foreach (var entity in entities)
    this.Entities.Remove(entity); this._context.SaveChanges();
    }
    catch (DbEntityValidationException dbEx)
    {
    throw new Exception(GetFullErrorText(dbEx), dbEx);
    }
  • \src\Libraries\Nop.Core\WebHelper.cs(46)
            protected virtual Boolean IsRequestAvailable(HttpContextBase httpContext)
    {
    if (httpContext == null)
    return false; try
    {
    if (httpContext.Request == null)
    return false;
    }
    catch (HttpException)
    {
    return false;
    }
    return true;
    }
  • \src\Libraries\Nop.Core\Plugins\PluginManager.cs(434)
    try
    {
    File.Copy(plug.FullName, shadowCopiedPlug.FullName, true);
    }
    catch (IOException)
    {
    Debug.WriteLine(shadowCopiedPlug.FullName + " is locked, attempting to rename");
    //this occurs when the files are locked,
    //for some reason devenv locks plugin files some times and for another crazy reason you are allowed to rename them
    //which releases the lock, so that it what we are doing here, once it's renamed, we can re-shadow copy
    try
    {
    var oldFile = shadowCopiedPlug.FullName + Guid.NewGuid().ToString("N") + ".old";
    File.Move(shadowCopiedPlug.FullName, oldFile);
    }
    catch (IOException exc)
    {
    throw new IOException(shadowCopiedPlug.FullName + " rename failed, cannot initialize plugin", exc);
    }
    //ok, we've made it this far, now retry the shadow copy
    File.Copy(plug.FullName, shadowCopiedPlug.FullName, true);
    }