你可以用这个。
步骤 1. 将文件扩展名映射到 TransferRequestHandler
IIS 7 集成模式使用 HTTP 处理程序映射,将路径/动词组合指向 HTTP 处理程序。例如,有一个默认处理程序映射,它将 path="*.axd" verb="GET,HEAD,POST,DEBUG" 指向相应的 ISAPI 模块,以用于站点运行的 .NET 运行时版本。在 IIS Express 下查看默认处理程序的最简单方法是在 IIS Express 下运行一个站点,右键单击系统托盘中的 IIS Express 图标,单击“显示所有应用程序”,然后单击一个站点。底部的 applicationhost.config 链接已链接,因此您只需单击它即可将其加载到 Visual Studio 中。
如果您滚动到底部,您会看到path="*" verb="*" 有一个包罗万象的StaticFile 映射,它指向StaticFileModule,DefaultDocumentModule,DirectoryListingModule。如果您什么都不做,那将处理您的 .html 请求。所以第一步是在你的 web.config 中添加一个处理程序,它将*.html 请求指向TransferRequestHandler。 TransferRequestHandler 是处理您习惯于在 MVC 路由中看到的无扩展 URL 的处理程序,例如/store/details/5.
添加处理程序映射非常简单 - 只需打开您的 web.config 并将其添加到 <system.webServer/handlers> 节点即可。
<add name="HtmlFileHandler" path="*.html" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
请注意,如果您愿意,可以使路径更具体。例如,如果你只想拦截一个特定的请求,你可以使用 path="sample.html"
步骤 2. 配置路线
接下来,您需要一条新路线。打开App_Start/RouteConfig.cs 并拨打RegisterRoutes 电话。我完整的RegisterRoutes 看起来像这样:
routes.MapRoute(
name: "XMLPath",
url: "sitemapindex.xml",
defaults: new { controller = "Home", action = "Html", page = UrlParameter.Optional }
);
第 3 步。路由现有文件
这几乎涵盖了它,但还有一件事需要注意 - 覆盖与现有文件匹配的请求。如果您有一个名为 myfile.html 的实际文件,路由系统将不允许您的路由运行。我忘记了这一点,最后出现了 HTTP 500 错误(递归溢出),不得不向 Eilon Lipton 寻求帮助。
无论如何,这很容易解决 - 只需将 routes.RouteExistingFiles = true 添加到您的路线注册中。我完成的 RegisterRoutes 调用如下所示:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.RouteExistingFiles = true;
routes.MapRoute(
name: "CrazyPants",
url: "{page}.html",
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://sprint-newhomes.move.com/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://sprint-newhomes.move.com/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");
}