2015-05-19 73 views
1

我有我的本地IIS中的网站。 该网站的应用程序池处于Clasic模式。ASP.NET MVC路由,TransferRequestHandler不与ClassicMode与IIS工作

我正在将文件的路径映射到MVC上的路径。

我有路径登记这样

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

    //routes.RouteExistingFiles = true; 
    routes.MapRoute(
     name: "XMLPath", 
     url: "sitemapindex.xml", 
     defaults: new { controller = "Home", action = "Html", page = UrlParameter.Optional } 
    ); 

    routes.MapRoute(
     name: "Default", 
     url: "{controller}/{action}/{id}", 
     defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
    ); 
} 

在我的控制器我有这样的方法

public FileResult Html() 
{ 
    var stringBuilder = new StringBuilder(); 
    stringBuilder.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); 
    stringBuilder.AppendLine("<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">"); 
    stringBuilder.AppendLine("<sitemap>"); 
    stringBuilder.AppendLine("<loc>http://[site]/sitemaps/sitemap_01.xml</loc>"); 
    stringBuilder.AppendLine("<lastmod>" + DateTime.Now.ToString("MMMM-dd-yyyy HH:mm:ss tt") + "</lastmod>"); 
    stringBuilder.AppendLine("</sitemap>"); 
    stringBuilder.AppendLine("<sitemap>"); 
    stringBuilder.AppendLine("<loc>http://[site]/sitemaps/sitemap_02.xml</loc>"); 
    stringBuilder.AppendLine("<lastmod>" + DateTime.Now.ToString("MMMM-dd-yyyy HH:mm:ss tt") + "</lastmod>"); 
    stringBuilder.AppendLine("</sitemap>"); 
    stringBuilder.AppendLine("</sitemapindex>"); 

    var ms = new MemoryStream(Encoding.ASCII.GetBytes(stringBuilder.ToString())); 

    Response.AppendHeader("Content-Disposition", "inline;filename=sitemapindex.xml"); 

    return new FileStreamResult(ms, "text/xml"); 
} 

在我web.cong我添加

<handlers> 
    <add name="HtmlFileHandler" path="*.xml" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="classicMode,runtimeVersionv4.0" /> 
    </handlers></system.webServer> 
<runtime> 

所有这此类似于 http://weblogs.asp.net/jongalloway/asp-net-mvc-routing-intercepting-file-requests-like-index-html-and-what-it-teaches-about-how-routing-works

唯一的区别是使用preCondition =“classicMode,runtimeVersionv4.0”not preCondition =“integratedMode,runtimeVersionv4.0”。

如果我在Visual Studio 2013中运行项目工作完美,但是当我在IIS中运行它时不起作用。如果我更改我的IIS的AppPool以集成模式工作。 但我需要在经典模式下拥有AppPool。

当我运行路线(URL)[现场] /sitemapindex.xml在经典模式我得到这个错误:

HTTP错误500.21 - 内部服务器错误 处理程序“HtmlFileHandler”有一个坏模块“ManagedPipelineHandler”在其模块列表中

这是我使用的Example Project。 如何在IIS网站的classicMode中使用System.Web.Handlers.TransferRequestHandler?

回答

3

如果你看看TransferRequestHandler的源代码,它清楚地知道它需要应用程序在集成模式下运行。如果它能够将WorkerRequest作为IIS7WorkerRequest进行强制转换,那么它意味着它在集成模式下运行。

internal class TransferRequestHandler : IHttpHandler {   
    public void ProcessRequest(HttpContext context) { 
     IIS7WorkerRequest wr = context.WorkerRequest as IIS7WorkerRequest; 
     if (wr == null) { 
      throw new PlatformNotSupportedException(SR.GetString(SR.Requires_Iis_Integrated_Mode)); 
     }    
     wr.ScheduleExecuteUrl(null, 
           null, 
           null, 
           true, 
           context.Request.EntityBody, 
           null, 
           preserveUser: false);    
     context.ApplicationInstance.EnsureReleaseState(); 
     context.ApplicationInstance.CompleteRequest(); 
    } 
    public bool IsReusable { 
     get { 
      return true; 
     } 
    }} 
0

帕布洛,你仍然可以通过只是改变你的web.config文件中使用的HttpHandler的部分,而不是处理区域映射在IIS7经典模式下使用的处理程序。以下是说明:https://msdn.microsoft.com/en-us/library/46c5ddfy.aspx

您必须这样做的原因是,旧版II中的经典IIS管道模式使用了独立的ISAPI扩展,该扩展位于II和.NET运行时/工作进程之间。在较新的IIs7集成模式下,ISAPI扩展消失并管理.NET工作进程本身内的处理程序。由于您将较旧的经典ISAPI扩展管道部分与较新的IIs7服务器设置混合在一起,因此必须通过较早的web.config设置来管理该处理程序设置。