0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I think you should mention this new route should be above the default ones. because the order matters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.