Question

I was wondering how would one go about returning XML as a result using ASP.Net MVC when the user enters the following url:

http://www.mysite.com/people.xml

If the user enters http://www.mysite.com/people the normal html view should be rendered showing all the people in the database whereas if they add .xml they will get xml containing all the people in the database.

The 37Signals' Highrise API works this way. I know I can use XmlResult but, how would I configure the action to return the normal view if the user does not specify .xml at the end of the url?

Was it helpful?

Solution

If I understood your question right, I think you can solve your problem like this:

public class HomeController : Controller{

    public ActionResult Index(string filename){

        if(filename != null){
            string ext = // parse the filename and get the extension
            /*
                can't test, but I think 
                System.IO.Path.GetExtension(filename);
                should work
            */

            if(ext == "xml"){
                //  do stuff

                return new XmlResult(/* filepath or something */);
            }

        }

        //  do stuff

        //  return the view you want if no filename or not a xml extension
        return View();
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top