0

Language: C# 5.0
Platform: ASP.Net 4.5 on IIS 7.5
Operating System: Windows Server 2008 R2 Standard 64-bit

I am working on an old Web Forms application to add XML Sitemaps which are dynamically generated from our database. I created a generic handler (.ashx) and am building XML with a XmlWriter. I called site.com/SitemapIndex.ashx and voila! So far so good.

I have a business requirement that the sitemap end with the .xml extension. I added a simple URL rewrite rule to my web.config and my local rig processes SitemapIndex.xml through the SitemapIndex.ashx file.

When I deploy the solution to our staging server, I got a 404 not found error on the SitemapIndex.xml file. But the .ashx still worked, so I added a handler mapping on that server. I used a Managed Handler for the path *.xml that uses the System.Web.UI.SimpleHandlerFactory. When I requested SitemapIndex.xml again the output rendered correctly.

Then I tried to request a physical XML file on the server and received the following error message: "There is no build provider registered for the extension '.xml'. You can register one in the section in machine.config or web.config. Make sure is has a BuildProviderAppliesToAttribute attribute which includes the value 'Web' or 'All'."

For now I can process .xml files dynamically through my .ashx handlers, but real xml files will no longer be served. I need to do both. Any help much appreciated!

Gray
  • 115,027
  • 24
  • 293
  • 354
bopapa_1979
  • 8,949
  • 10
  • 51
  • 76

2 Answers2

0

I supposed the problem is "returning physical XML files"

So You should set your header information correctly.

In your ashx,

Content type should be

Content-Type: application/octet-stream

                 context.Response.ContentType = "application/octet-stream"; 
                 context.Response.AddHeader("Content-Transfer-Encoding", "binary");

                FileInfo fileInfo = new FileInfo(xmlFileName);
                long length = fileInfo.Length;

                context.Response.WriteFile(xmlFileName, 0, length);

I hope I understand the question well, there is nothing related with URLRewrite it is about relaying.

Davut Gürbüz
  • 5,526
  • 4
  • 47
  • 83
  • I could do this, but it would require writing another .ashx handler to handle all real .xml files on the server and adding another rewrite rule to route all non-specific XML files to this handler, which doesn't seem clean. What I'd really like to do is have MY sitemap handler handle URLs that match a sitemap, and let other URLs fall through to the actual file. – bopapa_1979 Apr 19 '13 at 15:13
0

The solution I used was to remove the Handler Mapping I had created for *.xml and just add one for the specific files I wanted ASP.Net to handle. Everything was already working fine on my dev rig, so I used a config transform on my staging and release configurations to add the handlers I wanted on deployment. The relevant section of my web.staging.config file looks like this:

<system.webServer>
  <!-- Handler mappings for our xml sitemaps-->
  <handlers>
    <add name="SimpleHandlerFactory-SitemapIndex" path="SitemapIndex.xml" verb="GET,HEAD,POST,DEBUG" type="System.Web.UI.SimpleHandlerFactory" resourceType="Unspecified" preCondition="integratedMode" xdt:Transform="Insert"  />
  </handlers>
</system.webServer>
bopapa_1979
  • 8,949
  • 10
  • 51
  • 76
  • I felt a little dumb when I figured this one out. OF COURSE *.xml will route all XML requests to the SimpleHandlerFactory the way I originally configured it. When did I get so lazy I don't notice wildcards? – bopapa_1979 Apr 19 '13 at 15:41