C#MVC3重定向到的sitemap.xml控制器动作 [英] C# mvc3 redirect sitemap.xml to controller action

查看:138
本文介绍了C#MVC3重定向到的sitemap.xml控制器动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要为www.domain.com/sitemap.xml作出一个控制器的动作在网站上的每个呼叫重定向。我怎样才能做到这一点?

我有了这个,直到至今,但​​是状态code返回是302我想它返回一个200状态,但仍然重定向/重写到一个控制器动作。我之所以要问的是,我希望每个呼吁重定向到一个controlleraction提到的网址。该控制器做一些富恩生成一个XML站点地图。然后需要此controlleraction的输出被返回。

 保护无效的Application_BeginRequest()
{
    //检查是否为sitemap.xml的一个已经调用
    如果(Request的==/sitemap.xml)
    {
        Response.RedirectToRoute(XmlSitemap);
    }
}


解决方案

您可以使用MVC的路由功能来实现这一点:

  routes.MapRoute(
    网站地图
    sitemap.xml的
    新{控制器=家,行动=网站地图}
);

有了这个寄予放置在Global.asax文件,以domain.com/sitemap.xml~~V所有请求都将被路由到主控制器的行动地图

I want to redirect each call on a website made for www.domain.com/sitemap.xml to a controller action. How can I achieve this?

I've got this till so far, but the statuscode returned is an 302. I want to return an 200 status with it, but still redirect / rewrite to an controller action. The reason i'm asking is that I want each call for the mentioned url redirected to a controlleraction. The controller does some foo en generates an xml sitemap. Then the output of this controlleraction needs to be returned.

protected void Application_BeginRequest()
{
    //Check if an call for the sitemap.xml has been made
    if (Request.Path == "/sitemap.xml")
    {
        Response.RedirectToRoute("XmlSitemap");
    }
}

解决方案

You can use MVC's routing feature to achieve this:

routes.MapRoute(
    "Sitemap",
    "sitemap.xml",
    new { controller = "Home", action = "Sitemap" } 
);

With this placed placed in your global.asax file, all requests to domain.com/sitemap.xml will be routed to the Home controller's Sitemap action.

这篇关于C#MVC3重定向到的sitemap.xml控制器动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆