gpt4 book ai didi

c# - 将静态 URL 路由到 MVC 中的 Controller 操作?

转载 作者:太空宇宙 更新时间:2023-11-03 10:30:08 25 4
gpt4 key购买 nike

大家好,我的 Controller 中有一个 aciton,它为我的站点生成一个站点地图,这里的问题是第一次当文件不存在时请求转到操作但是一旦生成文件,一个静态文件就会被放置在站点中,之后当我点击 URL 时,请求就不会发送到 Controller ,是否有解决这个问题的方法

Below is the URL

http://localhost:1234/sitemap.xml

Below is the code am using

[HttpGet, ActionName("sitemap")]
public ActionResult sitemap()
{

//will get the sitemap content here and will generate the sitemap
var sitemapFile = "~/sitemap.xml";
return File(sitemapFile, "text/xml");
}

每次我点击 URL 时,它都应该执行操作并重新生成文件,但这在这里不起作用任何人都可以帮助我吗

最佳答案

当您的 sitemap.xml 文件存在时,IIS 将接收请求并为您现有的 sitemap.xml 提供服务。有关此事的更多信息,请查看 this article

如果我理解正确,您想将 sitemap.xml 请求 路由到 MVC 路由,这里很好 blog post关于那个。

您首先需要做的是将sitemap.xml 的请求映射到TransferRequestHandler。将此添加到您的 web.config

<add name="SitemapFileHandler" path="sitemap.xml" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

接下来在 RouteConfig.cs 中配置路由:

routes.MapRoute(
name: "SiteMapRoute",
url: "sitemap.xml",
defaults: new { controller = "SEO", action = "Sitemap", page = UrlParameter.Optional }
);

并在你映射任何路线之前放置此语句

routes.RouteExistingFiles = true;

有关 RouteExistingFiles 的更多信息

关于c# - 将静态 URL 路由到 MVC 中的 Controller 操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30482648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com