将 sitemap.xml http 请求传输到 ASP.NET MVC 中的路由处理程序
我正在尝试添加一条路由,将所有 sitemap.xml
请求传输到我创建的自定义请求处理程序。
我尝试使用以下代码:
routes.Add(new Route("sitemap.xml", new Helpers.SiteMapRouteHandler()));
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
但是当我使用 Url.Action()
创建链接时:
Url.Action("Index", new { controller = "About"})
当我尝试导航到 XML 文件时,我得到以下信息:
/sitemap.xml?action=Index&controller=About
我做错了什么?
答案:
我使用了这个解决方案:
I'm trying to add a route that will transfer all sitemap.xml
requests to a custom request handler I made.
I tried using the following code:
routes.Add(new Route("sitemap.xml", new Helpers.SiteMapRouteHandler()));
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
But when I make a link using Url.Action()
:
Url.Action("Index", new { controller = "About"})
I get the following when I try to navigate to the XML file:
/sitemap.xml?action=Index&controller=About
What am I doing wrong?
ANSWER:
I used this solution:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想路由到请求处理程序并执行操作,
路由
您可以添加如下所示的 - MVC:如何将 /sitemap.xml 路由到 ActionResult? 它对我有用
更新: 还要确保
已设置。If you want to route to and action instead to a request handler
You could add a route like this
as mentioned here - MVC: How to route /sitemap.xml to an ActionResult? and it worked for me
Update: Also ensure that
<modules runAllManagedModulesForAllRequests="true">
is set.我不确定这是否能解决问题,但值得一试:
I'm not sure if this will solve the problem, but it's worth a try:
您必须将处理程序添加到 web.config 文件。
并配置您的路线:
在此处阅读更多信息:
You have to add a handler to web.config file.
And configure your routes:
Read more here:
http://weblogs.asp.net/jongalloway//asp-net-mvc-routing-intercepting-file-requests-like-index-html-and-what-it-teaches-about-how-routing-works