我觉得面对这个问题完全是愚蠢的。 我在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,但可能会以某种方式号码不再受到号...
你为什么要这样做而不是绑定到模型? –
's'的内容是什么 –
嗯,一个值绑定到模型,但由JavaScript填充,所以我宁愿做模型属性字符串,然后在C#评估。第二个值通过Ajax调用和ist的时间在URL中传递,但是您不能将时间格式传递给URL,因此我使用JavaScript将0:00:00的值重新格式化为0_00_00,然后在c#中评估参数包含数字...... – PaNika