我有一個覆蓋ActionResult的SitemapActionResult,當http://www.sprelle.no/Home/SiteMap被擊中時會傳遞一個SEO sitemap.xml。到現在爲止還挺好。MVC:如何將/sitemap.xml路由到ActionResult?
但是,我想要的是在Google訪問/sitemap.xml時爲sitemap.xml服務。爲此,我需要一條看到「sitemap.xml」並指向/ Home/Site的路線。
如何創建此映射(在路由表中)?
我有一個覆蓋ActionResult的SitemapActionResult,當http://www.sprelle.no/Home/SiteMap被擊中時會傳遞一個SEO sitemap.xml。到現在爲止還挺好。MVC:如何將/sitemap.xml路由到ActionResult?
但是,我想要的是在Google訪問/sitemap.xml時爲sitemap.xml服務。爲此,我需要一條看到「sitemap.xml」並指向/ Home/Site的路線。
如何創建此映射(在路由表中)?
添加地圖:
routes.MapRoute(
"Sitemap",
"sitemap.xml",
new { controller = "Home", action = "SiteMap" }
);
注意,路線,控制器和操作選項是硬編碼。
你可以使用這個。
步驟1.映射文件擴展名來TransferRequestHandler
IIS 7集成模式下使用HTTP處理器映射該點的路徑/動詞組合到HTTP處理程序。例如,有一個默認的處理程序映射,它將path =「*。axd」verb =「GET,HEAD,POST,DEBUG」指向相應的.NET運行時版本的ISAPI模塊,該網站正在運行。查看IIS Express下默認處理程序的最簡單方法是在IIS Express下運行一個站點,右鍵單擊系統托盤中的IIS Express圖標,單擊「顯示所有應用程序」,然後單擊一個站點。底部的applicationhost.config鏈接已鏈接,因此您可以單擊它並將其加載到Visual Studio中。
如果您滾動到最下方,您會看到path="*" verb="*"
有一個指向StaticFileModule,DefaultDocumentModule,DirectoryListingModule
的超級鏈接StaticFile映射。如果你什麼也不做,這就是處理你的.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" />
請注意,如果您願意,您可以使路徑更具體。舉例來說,如果你只是想攔截一個特定的請求,您可以使用路徑=「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");
}
雖然這個答案可能是正確的,但最好在這裏包含相關的代碼。如果網站移動或被刪除,僅鏈接答案可能會變得不正確。 – Jonathan
爲了得到這個工作,你需要做兩件事情:
<system.webServer>
<handlers>
<!-- add the following line -->
<add name="SitemapXml" path="sitemap.xml" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/>
</handlers>
</system.webServer>
routes.MapRoute(
name: "Sitemap",
url: "sitemap.xml",
defaults: new { controller = "YourControllerName", action = "YourActionName" }
);
其實我這個今天早些時候嘗試,但不能得到它的工作,因爲我加入_after_缺省路由。它需要在默認路由前插入才能工作。謝謝。 – HaakonL
還記得要在system.webServer中爲模塊配置添加runAllManagedModulesForAllRequests =「true」,否則它會嘗試使用staticfile處理程序,它將返回一個404 –
@RaulVejar謝謝!我在互聯網上搜索了一個多小時,並且你是第一個提到這個模塊的人。它解決了我的問題。 – ddilsaver