c# - ASP.NET MVC 路由、TransferRequestHandler 不能在 IIS 的经典模式下工作

标签 c# asp.net-mvc asp.net-mvc-3 iis

我在本地 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 }
    );
}

在我的 Controller 中我有这个方法

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"而不是 preCondition="integratedMode,runtimeVersionv4.0"。

如果我在 Visual Studio 2013 中运行该项目,效果很好,但当我在 IIS 中运行时,它就无法运行。如果我将 IIS 的 AppPool 更改为集成模式,则可以正常工作。 但我需要让 AppPool 处于经典模式。

当我在经典模式下运行 Route (url) [site]/sitemapindex.xml 时出现此错误:

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

这是一个 Example Project我正在使用。 我如何在 IIS 站点的经典模式下使用 System.Web.Handlers.TransferRequestHandler?

最佳答案

如果您查看 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;
        }
    }}

关于c# - ASP.NET MVC 路由、TransferRequestHandler 不能在 IIS 的经典模式下工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30329483/

相关文章:

asp.net-mvc-3 - 通过单击两次提交避免在 Asp.net MVC 中重复提交表单

asp.net-mvc-3 - ASP.NET MVC 3 中 ModelState.IsValid 的限制

c# - 在 sql 和 web 应用程序之间发送安全密码的最佳实践?

c# - AutoComplete 控件调用具有空值的 Controller

c# - PdfPTable单元格宽度

asp.net-mvc - 在 .NET MVC 3 中使用 REST Web 服务

jquery - 为什么 jquery 加载第二个,即使它是在 mvc bundle 配置文件的第一个位置定义的?

jquery - MVC3 复杂 JSON 列表绑定(bind)

c# - 授权角色 WebAPI oauth owin

c# - 如何在C#中遍历多层次数组