larryq
larryq

Reputation: 16299

Remap specific file/path in ASP.Net MVC

New to MVC routing, forgive the basic nature of the question.

Is it possible to configure the routing in ASP.Net MVC so that someone browsing to, say, http://www.mysite.com/sitemap.xml is redirected to a view that renders XML content? I assume so but am not sure of a good approach.

Upvotes: 0

Views: 373

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

You could register a route before your default route:

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

and then you could have a SitemapController:

public class SitemapController: Controller
{
    public ActionResult Index()
    {
        var model = ...

        // Don't look for XmlResult, it's up to you to write it
        return new XmlResult(model);
    }
}

Now when you navigate to /sitemap.xml, the Index action of the Sitemap controller will get executed.

Upvotes: 1

Related Questions