Asp.net 与MVC的链接问题
我是ASP.NET和MVC的不速之客,所以请容忍我,并提前感谢您的帮助-- 我试图链接到存储在应用程序根目录中的KML文件,但无法使其正常工作,请参见以下内容:Asp.net 与MVC的链接问题,asp.net,asp.net-mvc,Asp.net,Asp.net Mvc,我是ASP.NET和MVC的不速之客,所以请容忍我,并提前感谢您的帮助-- 我试图链接到存储在应用程序根目录中的KML文件,但无法使其正常工作,请参见以下内容: function initCB(instance) { ge = instance; ge.getWindow().setVisibility(true); var href = 'http://code.google.com/apis/earth/documen
function initCB(instance) {
ge = instance;
ge.getWindow().setVisibility(true);
var href = 'http://code.google.com/apis/earth/documentation/samples/kml_example2.kml';
var href2 = '~/cngKml.kml'
google.earth.fetchKml(ge, href2, kmlFinishedLoading);
}
当我将它指向'href'变量时,它会工作,但当我将它指向'href2'变量时,它不会加载任何内容
完整index.cshtml如下所示:
@{
ViewBag.Title = "STI";
string path = HttpUtility.JavaScriptStringEncode(HttpContext.Current.Server.MapPath("~/kml_example2.kml"));
}
<head>
<title>CNG</title>
<script type="text/javascript" src="https://www.google.com/jsapi"> </script>
<script type="text/javascript">
var ge;
var placemark;
var kmlObject;
google.load("earth", "1", { "other_params": "sensor=false" });
function init() {
google.earth.createInstance('map3d', initCB, failureCB);
}
function initCB(instance) {
ge = instance;
ge.getWindow().setVisibility(true);
var href = 'http://code.google.com/apis/earth/documentation/samples/kml_example2.kml';
var href2 = '@path';
google.earth.fetchKml(ge, href2, kmlFinishedLoading);
}
function kmlFinishedLoading(obj) {
kmlObject = obj;
if (kmlObject) {
if ('getFeatures' in kmlObject) {
kmlObject.getFeatures().appendChild(placemark);
}
ge.getFeatures().appendChild(kmlObject);
if (kmlObject.getAbstractView())
ge.getView().setAbstractView(kmlObject.getAbstractView());
}
}
function failureCB(errorCode) {
}
google.setOnLoadCallback(init);
</script>
</head>
@section featured {
<section class="featured">
<div class="content-wrapper">
<hgroup class="title">
<h1>@ViewBag.Title.</h1>
<h2>@ViewBag.Message</h2>
</hgroup>
</div>
</section>
}
<div id="gearth">
<div id="map3d" style="width:960px; height:640px; align-self:center;"></div>
</div>
@{
ViewBag.Title=“STI”;
string path=HttpUtility.JavaScriptStringEncode(HttpContext.Current.Server.MapPath(“~/kml\u example2.kml”);
}
压缩天然气
var-ge;
var-placemark;
var-kmlObject;
load(“earth”、“1”、“其他参数”:“sensor=false”});
函数init(){
createInstance('map3d',initCB,failureCB);
}
函数initCB(实例){
ge=实例;
ge.getWindow().setVisibility(true);
var href=http://code.google.com/apis/earth/documentation/samples/kml_example2.kml';
var href2='@path';
google.earth.fetchKml(ge、href2、kmlFinishedLoading);
}
功能kmlFinishedLoading(obj){
kmlObject=obj;
if(kmlObject){
if('getFeatures'在kmlObject中){
kmlObject.getFeatures().appendChild(placemark);
}
ge.getFeatures().appendChild(kmlObject);
if(kmlObject.getAbstractView())
ge.getView().setAbstractView(kmlObject.getAbstractView());
}
}
功能故障CB(错误代码){
}
setOnLoadCallback(init);
@特色栏目{
@ViewBag.Title。
@查看包。留言
}
生成的HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>STI</title>
<link href="/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<meta name="viewport" content="width=device-width" />
<link href="/Content/site.css" rel="stylesheet"/>
<script src="/Scripts/modernizr-2.6.2.js"></script>
</head>
<body>
<header>
<div class="content-wrapper">
<div class="float-left">
<p class="site-title"><a href="/">sti</a></p>
</div>
<div class="float-right">
<section id="login">
Hello, <a class="username" href="/Account/Manage" title="Manage">reecea</a>!
<form action="/Account/LogOff" id="logoutForm" method="post"><input name="__RequestVerificationToken" type="hidden" value="J0EkwX2027gRZ-gDCvH1WMHGpGUnW-Sl2m3jEOpKw2684DUjjywYCFBQ9pPNfJ93pyJIZ9XH9HLMYdFNiVcHohtNsvKA1sIiKf3tL3EekGI1" /> <a href="javascript:document.getElementById('logoutForm').submit()">Log off</a>
</form>
</section>
<nav>
<ul id="menu">
<li><a href="/">Home</a></li>
<li><a href="/Admin">Admin</a></li>
<li><a href="/Account/Manage">My Account</a></li>
</ul>
</nav>
</div>
</div>
</header>
<div id="body">
<section class="featured">
<div class="content-wrapper">
<hgroup class="title">
<h1>STI.</h1>
<h2>CNG Stations Map</h2>
</hgroup>
</div>
</section>
<section class="content-wrapper main-content clear-fix">
<head>
<title>CNG</title>
<script type="text/javascript" src="https://www.google.com/jsapi"> </script>
<script type="text/javascript">
var ge;
var placemark;
var kmlObject;
google.load("earth", "1", { "other_params": "sensor=false" });
function init() {
google.earth.createInstance('map3d', initCB, failureCB);
}
function initCB(instance) {
ge = instance;
ge.getWindow().setVisibility(true);
var href = 'http://code.google.com/apis/earth/documentation/samples/kml_example2.kml';
var href2 = 'c:\\users\\reecea\\documents\\visual studio 2013\\Projects\\CngStationMap\\CngStationMap\\kml_example2.kml';
google.earth.fetchKml(ge, href2, kmlFinishedLoading);
}
function kmlFinishedLoading(obj) {
kmlObject = obj;
if (kmlObject) {
if ('getFeatures' in kmlObject) {
kmlObject.getFeatures().appendChild(placemark);
}
ge.getFeatures().appendChild(kmlObject);
if (kmlObject.getAbstractView())
ge.getView().setAbstractView(kmlObject.getAbstractView());
}
}
function failureCB(errorCode) {
}
google.setOnLoadCallback(init);
</script>
</head>
<div id="gearth">
<div id="map3d" style="width:960px; height:640px; align-self:center;"></div>
</div>
</section>
</div>
<footer>
<div class="content-wrapper">
<div class="float-left">
<p>© 2014 - STI</p>
</div>
</div>
</footer>
<script src="/Scripts/jquery-1.8.2.js"></script>
<!-- Visual Studio Browser Link -->
<script type="application/json" id="__browserLink_initializationData">
{"appName":"Chrome","requestId":"9f6ba0e6c2d041d29dcc37b58bbb4ef4"}
</script>
<script type="text/javascript" src="http://localhost:54165/39bfdebe74ed45968bc576a815347820/browserLink" async="async"></script>
<!-- End Browser Link -->
</body>
</html>
性病
你好
性病。
CNG站地图
压缩天然气
var-ge;
var-placemark;
var-kmlObject;
load(“earth”、“1”、“其他参数”:“sensor=false”});
函数init(){
createInstance('map3d',initCB,failureCB);
}
函数initCB(实例){
ge=实例;
ge.getWindow().setVisibility(true);
var href=http://code.google.com/apis/earth/documentation/samples/kml_example2.kml';
var href2='c:\\users\\reecea\\documents\\visualstudio 2013\\Projects\\CngStationMap\\CngStationMap\\kml\u example2.kml';
google.earth.fetchKml(ge、href2、kmlFinishedLoading);
}
功能kmlFinishedLoading(obj){
kmlObject=obj;
if(kmlObject){
if('getFeatures'在kmlObject中){
kmlObject.getFeatures().appendChild(placemark);
}
ge.getFeatures().appendChild(kmlObject);
if(kmlObject.getAbstractView())
ge.getView().setAbstractView(kmlObject.getAbstractView());
}
}
功能故障CB(错误代码){
}
setOnLoadCallback(init);
&抄袭;2014年-STI
{“appName”:“Chrome”,“requestId”:“9f6ba0e6c2d041d29dcc37b58bbb4ef4”}
Web.Config文件:
<system.webServer>
<staticContent>
<remove fileExtension=".kml"/>
<mimeMap fileExtension=".kml" mimeType="application/vnd.google-earth.kml+xml"/>
</staticContent>
尝试更改:
var href2 = path;
为此:
var href2 = @path;
如果没有@char,它将被视为简单的JS变量
顺便说一下,如果您在C#中定义变量,然后在JavaScript中使用它,请不要忘记使用HttpUtility.JavaScriptStringEncode
:
string path = HttpUtility.JavaScriptStringEncode(HttpContext.Current.Server.MapPath("~/cngKml.km"));
只需尝试
var href2='/cngKml.kml'
不能为链接使用物理文件路径(即Server.MapPath(“~/cngKml.kml”)
)
您不能在服务器需要解析的客户端代码中使用路径(例如,“~/anything”
)
您不能在C
中声明变量,然后像您尝试的那样在JavaScript中使用它。在上面的例子中,您至少需要使用“@path”
(尽管它仍然不起作用)
此外,请验证导航到web服务器上的
kml
文件是否正常工作,因为IIS通常不提供未知文件类型。如果这是一个问题,您可以使用类似于的web.config条目进行修复。为什么您说不能使用物理文件路径进行链接?我试过了,但效果很好。@Vitaliy因为我无法想象将服务器的物理文件路径传递给客户端会有什么好处。我看到很多初学者试图将其用于imagesrc
等,我正试图帮助一个自称为“noob to ASP.NET”的人,告诉他不应该这样做。相对或绝对url是正确的。物理文件路径不可用。请将您的代码发布到web服务器,并让我知道Z:\somepath\cnhKml.kml
是否适用于客户端。啊,我没有正确理解您。我以为你的意思是MapPath的代码不起作用。说得通,我同意你的看法。您的答案实际上是正确的。我做了您建议的更改,然后将mime类型添加到Web.Config文件中。现在google earth根本不会加载:-(我遗漏了什么?使用调试器查看路径变量的值是多少?是否只修改了路径变量?或者