jennifer samuel
jennifer samuel

Reputation: 1

Display string in xml format in browser either using view or controller

I have a string in my model.The string is actually XML content. I have a link on my page, when clicked it opens a new tab and displays the text as XML.

The result should be the same as when I right click on an xml file and open with Internet Explorer. The difference is that this is no file, its text that I need to display as XML in a new tab.

Anyone have an idea how to achieve this without creating a file and without giving a path to a file.

Upvotes: 0

Views: 916

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038780

You could have a controller that will serve this XML and set the proper content type header:

public class MyXMLController: Controller
{
    public ActionResult Index()
    {
        MyModel model = GetModelFromSomewhere(...);
        return Content(model.StringPropertyContainingXML, "text/xml");
    }
}

now all that's left is to write an anchor link pointing to /myxml/index:

@Html.ActionLink("Click to open XML", "index", "myxml", null, new { _target = "blank" })

Upvotes: 1

Related Questions