How do I generate a KML file in ASP.NET?

后端 未结 1 487
傲寒
傲寒 2021-02-19 06:08

How do I generate and return a KML document directly to the browser without writing a temporary file to the server or relying on a 3rd party library or class?

相关标签:
1条回答
  • 2021-02-19 06:39

    I suggest you consider using an HTTP Handler instead of a ASP.NET page. It will be cleaner and more performant. Just add new item of type "Generic Handler" to your project and consider moving the code to its ProcessRequest method. The general approach is good, though.

    By the way, unless you are explicitly mapping .kml files to an ASP.NET handler, it'll not run anyway. I suggest going with the default .ashx extension and add a Content-Disposition HTTP header to set the filename for the client:

    Response.AddHeader("Content-Disposition", "attachment; filename=File.kml");
    

    Also, note that you should set header stuff before anything is sent to the client so you should move setting Content-Type and adding header before other stuff.


    Full Solution (From the OP):

    Here's how I did it:

    Server

    1. Add the .kml mimetype to the folder where you want this "file" to live. Say, \\myDevServer\...\InetPub\KML
      (Google's instructions are only for Apache)
      1. Open Internet Information Services (IIS) Manager on your DEV server
      2. Navigate to your DEV site
      3. Right-click the KML folder and choose Properties
      4. Click the HTTP Headers tab
      5. Click the MIME types button
      6. Click New
      7. Enter
        • Extension: .kml
        • MIME Type: application/vnd.google-earth.kml+xml
      8. Click OK twice to get back to the HTTP Headers tab
    2. Set the KML folder as an ASP.NET application (maybe optional depending on how your server is set up)
      1. Click the Directory tab
      2. Click the Create button
      3. The Application name field becomes active with the setting KML
      4. Click OK taking you back to the main IIS Manager window

    Website

    1. Open VS2008:
      1. File >> New Website
      2. Choose:
        • Empty Web Site
        • Language: C#
        • Location: \\myDevServer\...\InetPub\KML\
    2. In Solution Explorer
      1. Rightclick the website
      2. Choose New Item
      3. Choose Generic Handler from the Visual Studio installed templates window
      4. Enter a name (I used MelroseVista.ashx )
      5. Choose Language: Visual C#
      6. Click OK
    3. Paste the following code

    //

    using System;
    using System.Web;
    using System.Xml;
    
    public class Handler : IHttpHandler
    {
        public void ProcessRequest( HttpContext context)
        {
            context.Response.ContentType = "application/vnd.google-earth.kml+xml";
            context.Response.AddHeader("Content-Disposition", "attachment; filename=MelroseVista.kml");
    
            XmlTextWriter kml = new XmlTextWriter(context.Response.OutputStream, System.Text.Encoding.UTF8);
    
            kml.Formatting = Formatting.Indented;
            kml.Indentation = 3;
    
            kml.WriteStartDocument();
    
            kml.WriteStartElement("kml", "http://www.opengis.net/kml/2.2");
            kml.WriteStartElement("Placemark");
            kml.WriteElementString("name", "Melrose Vista   FL");
            kml.WriteElementString("description", "A nice little town");
    
            kml.WriteStartElement("Point");
    
            kml.WriteElementString("coordinates", "-80.18451400000000000000,26.08816400000000000000,0");
    
            kml.WriteEndElement(); // <Point>
            kml.WriteEndElement(); // <Placemark>
            kml.WriteEndDocument(); // <kml>
    
            kml.Close();
    
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
    
    1. Attempt to load your page in your favorite browser
    2. You should get a popup asking you to open or save the resulting KML file.
    3. If you open it, you should have GoogleEarth launch itself and zoom to a thumbtack in Eastern Florida
    4. If you save it, you should see the following in the file

    \

    <?xml version="1.0" encoding="utf-8"?>
    <kml xmlns="http://www.opengis.net/kml/2.2">
       <Placemark>
          <name>Melrose Vista   FL</name>
          <description>A nice little town</description>
          <Point>
             <coordinates>-80.18451400000000000000,26.08816400000000000000,0</coordinates>
          </Point>
       </Placemark>
    </kml>
    

    Note: XmlTextWriter worked pretty well here. However, I think XMLDocument looks more promising for larger KML files since you can manipulate it in memory before pushing it to the user. If, for example, you want the same point to appear in multiple folders in the GoogleEarth Locations tree.

    0 讨论(0)
提交回复
热议问题