c# String.Format 抛出 "Invalid input exception"
c# String.Format throws "Invalid input exception"
我正在编写一个 c# 控制台程序,它读取一个配置文件和一个 google 找到的地图示例 in google dev website.。我将 html 内容复制到本地模板文件中,并为 string.format
放置了占位符(注意下面代码片段中的 {1}、{2}。
然后我从本地文件系统读取模板并使用下面的代码填充占位符。问题是 string.format
一直抛出错误 Input string was not in a correct format.
如果我用空白文件替换模板,则不会抛出 Input string was not in a correct format.
。没有例外。因此,问题出在下面的 html 模板而不是代码。有人可以发现 make 模板的问题,使其不适用于 string.format
吗?我猜它与 {
和 }
有关,所以我移动了任何双精度(即 {{
),但这没有帮助。
代码
var res = string.Format(template,
ConfigurationManager.AppSettings["PageTitle"],
ConfigurationManager.AppSettings["zoom"],
ConfigurationManager.AppSettings["CenterLat"],
ConfigurationManager.AppSettings["CenterLongi"],
ConfigurationManager.AppSettings["mapTypeId"],
sb.ToString(),
ConfigurationManager.AppSettings["strokeColor"],
ConfigurationManager.AppSettings["strokeOpacity"],
ConfigurationManager.AppSettings["strokeWeight"],
ConfigurationManager.AppSettings["fillColor"],
ConfigurationManager.AppSettings["fillOpacity"],
ConfigurationManager.AppSettings["GoogleMapBaseUrl"].Replace("_KEY_", ConfigurationManager.AppSettings["APIKey"])
);
HTML 模板
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>{0}</title>
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: {1},
center: {
lat: {2}, lng: {3}
},
mapTypeId: {4}
});
// Define the LatLng coordinates for the polygon's path.
var triangleCoords = [
{5}
];
// Construct the polygon.
var bermudaTriangle = new google.maps.Polygon(
{
paths: triangleCoords,
strokeColor: '{6}',
strokeOpacity: {7},
strokeWeight: {8},
fillColor: '{9}',
fillOpacity: {10}
});
bermudaTriangle.setMap(map);
}
</script>
<script async defer
src="{11}"></script>
</body>
</html>
您的字符串中大括号作为文字的存在导致 string.Format 解析失败。您应该在模板中使用此语法
(仅显示部分代码)
function initMap() {{
var map = new google.maps.Map(document.getElementById('map'), {{
zoom: {1},
center: {{
lat: {2}, lng: {3}
}},
mapTypeId: {4}
}});
我正在编写一个 c# 控制台程序,它读取一个配置文件和一个 google 找到的地图示例 in google dev website.。我将 html 内容复制到本地模板文件中,并为 string.format
放置了占位符(注意下面代码片段中的 {1}、{2}。
然后我从本地文件系统读取模板并使用下面的代码填充占位符。问题是 string.format
一直抛出错误 Input string was not in a correct format.
如果我用空白文件替换模板,则不会抛出 Input string was not in a correct format.
。没有例外。因此,问题出在下面的 html 模板而不是代码。有人可以发现 make 模板的问题,使其不适用于 string.format
吗?我猜它与 {
和 }
有关,所以我移动了任何双精度(即 {{
),但这没有帮助。
代码
var res = string.Format(template,
ConfigurationManager.AppSettings["PageTitle"],
ConfigurationManager.AppSettings["zoom"],
ConfigurationManager.AppSettings["CenterLat"],
ConfigurationManager.AppSettings["CenterLongi"],
ConfigurationManager.AppSettings["mapTypeId"],
sb.ToString(),
ConfigurationManager.AppSettings["strokeColor"],
ConfigurationManager.AppSettings["strokeOpacity"],
ConfigurationManager.AppSettings["strokeWeight"],
ConfigurationManager.AppSettings["fillColor"],
ConfigurationManager.AppSettings["fillOpacity"],
ConfigurationManager.AppSettings["GoogleMapBaseUrl"].Replace("_KEY_", ConfigurationManager.AppSettings["APIKey"])
);
HTML 模板
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>{0}</title>
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: {1},
center: {
lat: {2}, lng: {3}
},
mapTypeId: {4}
});
// Define the LatLng coordinates for the polygon's path.
var triangleCoords = [
{5}
];
// Construct the polygon.
var bermudaTriangle = new google.maps.Polygon(
{
paths: triangleCoords,
strokeColor: '{6}',
strokeOpacity: {7},
strokeWeight: {8},
fillColor: '{9}',
fillOpacity: {10}
});
bermudaTriangle.setMap(map);
}
</script>
<script async defer
src="{11}"></script>
</body>
</html>
您的字符串中大括号作为文字的存在导致 string.Format 解析失败。您应该在模板中使用此语法
(仅显示部分代码)
function initMap() {{
var map = new google.maps.Map(document.getElementById('map'), {{
zoom: {1},
center: {{
lat: {2}, lng: {3}
}},
mapTypeId: {4}
}});