2016-09-23 15 views
-1

我觉得面对这个问题完全是愚蠢的。 我在MVC应用程序中有简单的C#代码,我需要将字符串评估为有效的int,将另一个字符串评估为有效的DateTime。从Chrome,Opera,Firefox调用应用程序时,一切正常。但是当从IE或Edge(在多台PC上)调用应用程序时,评估失败。int.TryParse不适用于IE或Edge发布的值

我的代码:

if(!int.TryParse(s, out i)) 
    { 
     // something 
    } 

我使用的CultureInfo和的NumberFormatInfo没有试过效应。 字符串值为niether null也不为空并包含文本格式的int值。 与评估字符串为DateTime相同的行为。 真的不知道问题在哪里... 感谢您的帮助。

所有代码: 1.Controller

[HttpGet] 
    [Route("RatingDetail/{ratingID:int}/{time}/{value:int}")] 
    public JsonResult RatingDetail(int ratingID, string time, int value) 
    { 
     try 
     { 
      using (var context = new SenzorikaEntities()) 
      { 
       var rd = new RatingDB(); 
       // URL must not contain ':', so we are sending time 
       // in format 0_00_00 instead of 0:00:00. 
       // So we have to replace back... 
       time = time.Replace('_', ':'); 

       // This line fails when browsing in IE 
       // and yes sould by .TryParse, I know 
       var timeDB = DateTime.Parse(System.DateTime.Now.ToShortDateString() + " " + time); 

       var detialID = rd.InsertRatingDetail(context, ratingID, timeDB, value); 
       return Json(detialID, JsonRequestBehavior.AllowGet); 

      } 
     } 
     catch (Exception ex) 
     { 
      ErrorLog.LogError(ex, "Error when saving values."); 
      throw; 
     } 
    } 
  • 的Javascript

    function saveRating(percent, time) { 
         // model ID 
         var rid = $("#RatingID").val(); 
         var timeConvert = time.replace(':', '_').replace(':', '_'); 
    } 
    
    
    
    
    $.ajax(
        { 
         statusCode: { 
          500: showError('some error text'), 
          200: showError('') 
         }, 
         url: '/RatingDetail/' + rid + '/' + timeConvert + '/' + percent, 
         async: true, 
         type: 'GET', 
         dataType: 'json', 
         success: 
         function (response) { 
          if (response == undefined || response == "0") 
           showError('some error text'); 
         }, 
         error: 
          function (response) { 
           showError("); 
    
          } 
        }); 
    
  • 一个更有趣的点。我通过Fiddler追踪了这个页面。 Google中的GET请求(由AJAX调用)显示为正常:/ RatingDetail/69/0_00_01/4 但在边缘:/ RatingDetail/70 /%E2%80%8E0%E2%80%8E_%E2%80%8E00% E2%80%8E_%E2%80%8E05/9 的URL进行解码,早在控制器automaticaly,但可能会以某种方式号码不再受到号...

    +0

    你为什么要这样做而不是绑定到模型? –

    +2

    's'的内容是什么 –

    +0

    嗯,一个值绑定到模型,但由JavaScript填充,所以我宁愿做模型属性字符串,然后在C#评估。第二个值通过Ajax调用和ist的时间在URL中传递,但是您不能将时间格式传递给URL,因此我使用JavaScript将0:00:00的值重新格式化为0_00_00,然后在c#中评估参数包含数字...... – PaNika

    回答

    1

    我发现culprite ... 我做了数组形成我需要评估为int的字符串,我看到来自IE或Edge的响应有更多的字符,然后从Chrome/Opera/Firefox响应。 来自的回复IE已添加引号字符。 你可以在图片上看到。 IE的输入字符串为0:00:10,Chrome的输入字符串为0:00:12。

    Response from Chrome - everything OK:

    Response from IE/Edge - added quotation marks chars

    正如你看到的,我不能让从包含额外的引号串的时间跨度(或日期时间)格式。但是,这是不可见的调试时,直到我做了一个字符数组...

    所以我不得不作出解决方法,并清除额外的引号字符。

    public static string IEHackforTime(string time) 
        { 
         string newTime = ""; 
         var arr = time.ToCharArray(); 
         foreach(var c in arr) 
         { 
          if (Char.IsNumber(c) || c==':') 
          { 
           newTime += c.ToString(); // No need of StringBuilder, array is small enough 
          } 
         } 
         return newTime; 
        } 
    
    相关问题